Spring/스프링 기반 REST API 개발
[스프링 기반 REST API 개발] 테스트 코드 리팩토링
주멘이
2021. 1. 9. 11:25
여러 컨트롤러 간의 중복 코드 제거하기
- 클래스 상속을 사용하는 방법
- @Ingore 어노테이션으로 테스트로 간주되지 않도록 설정
중복 코드를 모아둔 BaseControllerTest class를 만들고 extends 하여 사용하기 하기
// 중복 어노테이션
@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureRestDocs
@Import(RestDocsConfiguration.class) // bean import
@ActiveProfiles("test")
@Ignore // test로 간주되지 않도록
public class BaseControllerTest {
// 중복 의존성 주입
@Autowired
protected MockMvc mockMvc;
@Autowired
protected ObjectMapper objectMapper;
@Autowired
protected ModelMapper modelMapper;
}
EventControllerTest와 IndexControllerTest는 extends BaseControllerTest
public class EventControllerTest extends BaseControllerTest {
}
public class IndexControllerTest extends BaseControllerTest {
}