본문 바로가기
Back-end/테스트

Spring Boot MVC 데이터베이스 통합 테스트 @Sql

by javapp 자바앱 2022. 10. 29.
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')

 

 


 

 

 

 

 

댓글