테스트 코드의 목적
품질 보증 : 백엔드의 API와 비즈니스 로직이 예상대로 작동하는지를 검증한다.
버그 발견 : 코드 변경 시 발생할 수 있는 문제를 조기에 발견하여 수정할 수 있다.
유지보수성 향상 : 기존 기능이 변경에 영향을 받지 않는지 확인하여 코드 유지보수를 쉽게 한다.
문서화 역할 : API의 동작 방식이나 비즈니스 로직을 문서화하는 데 도움을 준다.
테스트 코드의 유형주요 테스트 프레임워크
JUnit : Java의 표준 유닛 테스트 프레임워크로, Spring Framework와 함께 자주 사용된다.
Mockito : Java에서 의존성을 모의(mock) 객체로 대체할 수 있게 도와주는 라이브러리이다.
Spring Test : Spring 애플리케이션의 테스트를 지원하는 프레임워크로, 통합 테스트 및 웹 테스트에 유용하다.
Postman : API 테스트 도구로, HTTP 요청을 쉽게 보내고 응답을 검증할 수 있다.적용
//의존성 추가 testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'UserRestControllerTest
@SpringBootTest @Transactional @Rollback @AutoConfigureMockMvc class UserRestControllerTest { @Autowired private MockMvc mockMvc; @Test public void 회원가입_성공_테스트() throws Exception { String email = "email123@naver.com"; String password = "password123"; String username = "user01"; ResultActions resultActions = mockMvc.perform( MockMvcRequestBuilders .post("/studyNote/join") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .param("email", email) .param("password", password) .param("username", username) ); MvcResult resp = resultActions .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.redirectedUrl("/studyNote/index")) .andReturn(); }Ex)