1. 주요 애너테이션
애노테이션 | 목적 |
@BootstrapWith | 테스트 컨텍스트 부트스트래퍼를 커스터마이징할 때 사용 |
@ContextConfiguration | 테스트 컨텍스트를 구성할 설정을 지정 (XML, 자바 설정 클래스 등) |
@WebAppConfiguration | WebApplicationContext 로딩을 명시하여 웹 환경에서 테스트 MockServletContext 기반 설정 |
@ContextHierarchy | 부모-자식 컨텍스트 계층 구조를 정의 |
@TestPropertySource | 테스트용 프로퍼티 파일 또는 인라인 프로퍼티 지정 |
@TestBean (Spring 6.1+) | 기존 빈을 오버라이드하거나 테스트 전용 빈 정의 |
@MockitoBean | 기존 빈을 Mock 객체로 대체 |
@MockitoSpyBean | 기존 빈을 Spy 객체로 래핑 |
@DirtiesContext | 테스트 후 ApplicationContext 리셋 테스트 케이스에 의해 빈의 상태가 변경될 경우, 빈의 상태를 원복하기 위해 사용 테스트 간 독립성을 위해 사용됨 |
예시) @BootstrapWith
더보기
@BootstrapWith(MyCustomBootstrapper.class)
public class CustomBootstrapTest {
}
예시) @ContextConfiguration
더보기
@ContextConfiguration(classes = TestConfig.class)
// 또는
@ContextConfiguration(locations = "/test-context.xml")
public class AppContextTest {
}
예시) @WebAppConfiguration
더보기
@ContextConfiguration(classes = WebConfig.class)
@WebAppConfiguration("classpath:test-web-resources")
public class WebEnvTest {
}
예시) @ContextHierarchy
더보기
@ContextHierarchy({
@ContextConfiguration(name = "parent", classes = RootConfig.class),
@ContextConfiguration(name = "child", classes = WebConfig.class)
})
public class HierarchicalContextTest {
}
예시) @TestPropertySource
더보기
@TestPropertySource(locations = "classpath:/test.properties")
public class PropertyFileTest {
}
@TestPropertySource(properties = {
"spring.profiles.active=test",
"my.service.url=http://localhost"
})
public class InlinePropertyTest {
}
예시) @TestBean
더보기
@TestBean
MyRepository myRepository; // static 메서드에서 반환 필요
static MyRepository myRepository() {
return new InMemoryRepository(); // 테스트용 구현체
}
- 테스트에서 기존 빈을 대체하거나 새로 주입.
@TestBean(enforceOverride = true, contextName = "service")
MyService myService;
- enforceOverride로 강제할 수도 있음.
예시) @MockitoBean
더보기
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = DatabaseConfig.class)
public class MockitoBeanTest {
@MockitoBean
private LogRepository logRepository;
@Autowired
private LogService logService;
@Test
void test() {
when(logRepository.findById(1)).thenReturn("Hello Log");
assertEquals(logService.getMessageById(1), "Hello Log");
}
}
예시) @MockitoSpyBean
더보기
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = DatabaseConfig.class)
public class MockitoSpyTest {
@MockitoSpyBean
private LogService logService;
@Test
void test() {
doReturn("Hello Log").when(logService).getMessageById(anyInt());
assertEquals(logService.getMessageById(1), "Hello Log");
}
}
예시) @DirtiesContext
더보기
public class MyService {
private String msg = "Hello";
public void changeMessage(String msg) {
this.msg = msg;
}
public String getMessage() {
return msg;
}
}
@Configuration
public class MyServiceConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = MyServiceConfig.class)
public class DirtiesContextTest {
@Autowired
private MyService myService;
@Test
@DirtiesContext
void test() {
myService.changeMessage("Hello Spring");
assertEquals(myService.getMessage(),"Hello Spring");
}
@Test
void test2() {
assertEquals(myService.getMessage(),"Hello");
}
}
출처
'Spring > Spring Test' 카테고리의 다른 글
[Spring][Test] 2. Integration Testing (0) | 2025.04.21 |
---|---|
[Spring][Test] 1. Unit Testing (2) | 2025.04.19 |