
[ DAO (Data Access Object) 쪽 ]
5. test에서 BankDAOTest 테스트 해보기 [ given, when, then ]

본 코드에서 코드를 (DELETE, INSERT, UPDATE만. 이 친구들은 INT로 받으니까 세트 격) 다 짰으니, 실행이 잘 되는지 test 해보자
테스트 코드를 짜는 방식은 여러 개가 있는데
우리는 앞으로 본 코드를 작성하고,
걔가 잘 돌아가는지 코드를 테스트 파일에 옮겨서 !실행만! 해볼 것이다.
(테스트 주도 개발. TDD)
메인 메서드의 동작을 확인하기 위해서
단위 테스트(test)를 통해 각각의 메서드를 독립적으로 테스트하는 것
일단 DBConnectionTest를 test 해보자 (given, when, then)

우선 연결이 잘 되는지부터 확인해 볼 것!
1. given - 파라미터 (매개변수)

test엔 파라미터를 작성할 수 없다. 때문에, 이 given 주석 아래에 작성할 것! 물론 본코드에 매개 변수가 없으면 해당 부분 생략 가능. (지금은 파라미터 받은게 없으니 생략!)
2. when - 본코드의 메서드 호출
test엔 return 타입을 받지 못한다. 때문에, when 주석 아래에 return 타입을 작성할 것!

커넥션을 리턴 받네. 당연함.. getInstance니까

이렇게, 리턴타입을 적어주면 끝!
3. then - 검증 (우리는 초보라 눈검증으로 할 것)

main에 작성된 코드가 이거임. 이제 이 커넥션이 null인지 아닌지만 (작동하는지, 안하는지만) 검증하면 된다.


성공했다! 이런걸 확인 하는게 then!
개발할 때는 printStackTrace()로 하고,
개발을 다하면 저 피범벅 메세지를 바꾸면 됨. throw로?
이제 test에서 DELETE 해보자! (given, when, then)
1-1. given

given, when, then을 똑같이 해 볼 것이다. test엔 파라미터와 return 타입을 작성할 수 없다. 때문에, 이 주석들 아래에 작성할 것!

본코드에 매개 변수가 있는 것 확인!

여기에 본코드에 있는 매개변수 넣어주기
1-2. when


BankDAO가 static이 아니었으니까 new 해줘야함
return 타입 int인 것 확인한 후, 리턴 타입 적어주기
1-3. then

1이 아니면 다 실패한 거라고 치부할 것. result == 1

[ BankDAOTest deleteByNumber_test 전체 코드 ]
@Test public void deleteByNumber_test() { // given int number = 1; // when BankDAO dao = new BankDAO(); int result = dao.deleteByNumber(number); // then if(result == 1) { System.out.println("삭제 성공"); } else if (result == 0) { System.out.println(number + "번호를 찾을 수 없습니다."); } else { System.out.println("삭제 실패"); } }

test에서 UPDATE


number4의 balance가 100으로 바뀌었다. test 완료!
test 전체 코드
[ BankDAOTest ]
package dao; import org.junit.jupiter.api.Test; public class BankDAOTest { @Test public void deleteByNumber_test() { // given int number = 1; // when BankDAO dao = new BankDAO(); int result = dao.deleteByNumber(number); // then if(result == 1) { System.out.println("삭제 성공"); } else if (result == 0) { System.out.println(number + "번호를 찾을 수 없습니다."); } else { System.out.println("삭제 실패"); } } @Test public void insert_test() { //given String password = "535115"; int balance = 15000; //when BankDAO dao = new BankDAO(); int result = dao.insert(password, balance); //then if(result == 1) { System.out.println("insert 성공"); } else if (result == 0) { System.out.println(password + "와 " + balance + "를 넣을 수 없습니다"); } else { System.out.println("삭제 실패"); } } @Test public void updateByNumber_test() { //given int balance = 100; int number = 4; //when BankDAO dao = new BankDAO(); int result = dao.updateByNumber(balance, number); //then if (result == 1) { System.out.println("update 성공"); } else { System.out.println("업데이트 실패"); } } }
[ DBConnectionTest ]
package db; import org.junit.jupiter.api.Test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBConnectionTest { @Test public void getInstance_test() { // given = 파라미터 // when Connection conn = DBConnection.getInstance(); //then if(conn == null) { System.out.println("실패"); } else { System.out.println("성공"); } } }
Share article