728x90
Execution Sequence
@BeforeAll (static method)
@BeforeEach <<-- 샘플 데이터 삽입
@Test Method One
@AfterEach <<-- 샘플 데이터 삭제
@BeforeEach
@Test Method Two
@AfterEach
@AfterAll (static method)
test
StudentAndGradeServiceTest
@TestPropertySource("/application.properties")
@SpringBootTest
public class StudentAndGradeServiceTest
{
@Autowired
private StudentAndGradeService studentService;
@Autowired
private StudentDao studentDao;
@Autowired
private JdbcTemplate jdbc;
// 테스트 데이터 설정
@BeforeEach
public void setupDatabase(){
jdbc.execute("INSERT INTO student(id, firstname,lastname, email_address)"+
"values(1,'java','app','javapp@tistory)')");
}
// @Test
// public void createStudentService(){
// // TDD
// studentService.createStudent("java","app","javapp@tistory.com");
// // 완성하기 위해 코드 작성
// }
@Test
public void createStudentService(){
// TDD
// service 로 학생 생성
studentService.createStudent("java","app","javapp@tistory.com");
// dao로 email 로 찾기
CollegeStudent student = studentDao.findByEmailAddress("javapp@tistory.com");
assertEquals("javapp@tistory.com", student.getEmailAddress(), "이메일로 찾기");
}
@Test
public void isStudentNullCheck(){
}
@Test void deleteStudentServiceTest(){
Optional<CollegeStudent> deletedCollegeStudent=studentDao.findById(1);
assertTrue(deletedCollegeStudent.isPresent() ,"Return True");
studentService.deleteStudent(1);
deletedCollegeStudent = studentDao.findById(1);
assertFalse(deletedCollegeStudent.isPresent(),"Return False");
}
@Sql("/insertData.sql")
@Test
public void getGradeBookService(){
Iterable<CollegeStudent> iterableCollegeStudents =studentService.getGradebook();
List<CollegeStudent> collegeStudentList = new ArrayList<>();
for(CollegeStudent collegeStudent : iterableCollegeStudents){
collegeStudentList.add(collegeStudent);
}
assertEquals(5, collegeStudentList.size());
}
@AfterEach
public void setupAfterTransaction(){
jdbc.execute("DELETE FROM student");
}
}
@Sql
@Sql("/insertData.sql")
@Test
public void getGradeBookService(){
Iterable<CollegeStudent> iterableCollegeStudents =studentService.getGradebook();
List<CollegeStudent> collegeStudentList = new ArrayList<>();
for(CollegeStudent collegeStudent : iterableCollegeStudents){
collegeStudentList.add(collegeStudent);
}
assertEquals(5, collegeStudentList.size());
}
INSERT INTO student(id,firstname,lastname,email_address) VALUES(11,'Student','One','javapp@tistory.com')
INSERT INTO student(id,firstname,lastname,email_address) VALUES(12,'Student','Two','javapp@tistory.com')
INSERT INTO student(id,firstname,lastname,email_address) VALUES(13,'Student','Three','javapp@tistory.com')
INSERT INTO student(id,firstname,lastname,email_address) VALUES(14,'Student','Four','javapp@tistory.com')
'Back-end > 테스트' 카테고리의 다른 글
Spring Boot MVC 서비스 테스트 (0) | 2022.11.09 |
---|---|
Spring Boot MVC Controller Test (컨트롤러 테스트) (0) | 2022.11.05 |
Spring Boot Unit Testing - Mocking with Mockito - @MockBean, ReflectionTestUtils (0) | 2022.10.21 |
Spring Boot Unit Testing Support - 1 (0) | 2022.10.10 |
TDD 테스트 주도 개발 연습 (0) | 2022.10.05 |
댓글