TDD식 JUNIT 순서
1.테스트가 되려면 모듈화가 되어야한다.(책임 분리)
2.즉 테스트는 격리가 되야한다.(가짜 데이터를 집어 넣어 테스트 해볼 수 있다.)
3.테스트는 모든 경우의 수를 테스트 해야된다.(완벽할 순 없다)
하나의 머신에 대한 모든 경우의 수를 테스트를 해봐야한다.
4.통합 테스트를 해야한다.
결론:유기적으로 터지는 시스템을 잡을 수 있다.
모든 팀원들이 테스트 코드를 다짜야 된다.
무조건 테스트 할때는 h2를 써야한다 왜냐하면 일반 db를 쓰게 되면 나중에 테스트할 때
겹치는 데이터들이 생기기 때문이다.
JUNIT 비유 그림

머신을 두개로 만든 이유는 책임을 분리하려고다.
두개로 만든 이유는 디버깅 하기 편하기 때문이다.
컬렉션은 빈 데이터가 없더래도 null 값을 주지 않고 빈 배열 상태로 준다.
JUNIT 코드
package shop.mtcoding.blog.board;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import java.util.List;
@Import(BoardRepository.class) //내가 만든 클래스는 import 해줘야 함.
@DataJpaTest // DB 관련 객체들이 IoC에 뜬다.
public class BoardRepositoryTest {
@Autowired // Test에서 DI 하는 코드
private BoardRepository boardRepository;
@Test
public void delete(){
//given(객체의 바뀔 타겟점)
int id = 1;
//when
boardRepository.delete(id);
//then(상태)
List<Board> boardList = boardRepository.selectAll();
System.out.println(boardList.size());
Assertions.assertThat(boardList.get(0).getId()).isEqualTo(2);//결과 값 (삭제 하고나서 몇 번째 id가 시작점인지)
Assertions.assertThat(boardList.size()).isEqualTo(7);
}
@Test
public void update(){
//given(객체의 바뀔 타켓점)
String title = "새로운 제목";
String content = "새로운 내용";
int id =1;
//when
boardRepository.update(title,content,id);
//then(상태)
List<Board> boardList = boardRepository.selectAll();
System.out.println(boardList.size());
Assertions.assertThat(boardList.get(0).getTitle()).isEqualTo("새로운 제목");
Assertions.assertThat(boardList.get(0).getContent()).isEqualTo("새로운 내용");
}
@Test
public void selectAll_test(){
// given
// when
List<Board> boardList = boardRepository.selectAll();
System.out.println(boardList.size());
// then (id=1, title=제목1, content=내용1, author=홍길동)
// System.out.println(boardList);
Assertions.assertThat(boardList.get(0).getTitle()).isEqualTo("제목1");
Assertions.assertThat(boardList.get(0).getContent()).isEqualTo("내용1");
Assertions.assertThat(boardList.get(0).getAuthor()).isEqualTo("홍길동");
Assertions.assertThat(boardList.size()).isEqualTo(8);
}
@Test
public void selectOne_test(){
//given
int id = 1;
//when
Board board = boardRepository.selectOne(id);
//then(상태 검사)
//System.out.println(board);
Assertions.assertThat(board.getTitle()).isEqualTo("제목1");//실제로 실행 했을 때 리턴된 값을 넣어야됨
Assertions.assertThat(board.getContent()).isEqualTo("내용1");
Assertions.assertThat(board.getAuthor()).isEqualTo("홍길동");
}
@Test
public void insert_test(){ //테스트 메서드는 파라미터가 없다. 리턴도 없다.
// given
String title = "제목10";
String content = "내용10";
String author = "이순신";
// when
boardRepository.insert(title, content, author);
// then -> 눈으로 확인 (쿼리)
} // Rollback (자동)
}
Share article