Loading...
Spring Framework Reference Documentation 7.0.2의 Context Configuration with Component Classes의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
component class를 사용하여 test를 위한 ApplicationContext를 로드하려면 (참고:
Java-based container configuration), test
class에 @ContextConfiguration을 어노테이션으로 지정하고 classes attribute를 component class reference가 포함된 array로
설정하면 됩니다. 다음 예제는 그 방법을 보여 줍니다:
1@ExtendWith(SpringExtension.class) 2// ApplicationContext will be loaded from AppConfig and TestConfig 3@ContextConfiguration(classes = {AppConfig.class, TestConfig.class}) // (1) 4class MyTest { 5 // class body... 6}
| 1 | component class 지정. |
1@ExtendWith(SpringExtension::class) 2// ApplicationContext will be loaded from AppConfig and TestConfig 3@ContextConfiguration(classes = [AppConfig::class, TestConfig::class]) // (1) 4class MyTest { 5 // class body... 6}
| 1 | component class 지정. |
Component Classes<br>“component class”라는 용어는 다음 중 어느 것이든 가리킬 수 있습니다:<br>- @Configuration으로 어노테이션된 class.<br><br>- component (즉, @Component, @Service, @Repository 또는 기타 stereotype 어노테이션으로 어노테이션된 class).<br><br>- jakarta.inject 어노테이션으로 어노테이션된 JSR-330 compliant class.<br><br>- @Bean-method를 포함하는 모든 class.<br><br>- Spring component로 등록되도록 의도된 기타 모든 class (즉, ApplicationContext의 Spring bean)로서, Spring 어노테이션을 사용하지 않고도 단일 constructor의 automatic autowiring을 활용할 수 있는 class.<br><br>component class의 설정과 semantics에 대한 추가 정보는 @Configuration 및 @Bean의 javadoc을 참고하십시오. 특히 @Bean Lite Mode에 대한 논의에 주의를 기울이십시오. |
@ContextConfiguration 어노테이션에서 classes attribute를 생략하면,
TestContext framework는 default 설정 class의 존재를 감지하려고 시도합니다.
구체적으로, AnnotationConfigContextLoader와 AnnotationConfigWebContextLoader는
@Configuration javadoc에
명시된 대로 설정 class 구현에 대한 요구 사항을 충족하는 test class의 모든 static nested class를
감지합니다.
설정 class의 이름은 임의라는 점에 유의하십시오. 또한, 필요하다면 test class는
둘 이상의 static nested 설정 class를 포함할 수 있습니다. 다음
예제에서 OrderServiceTest class는 test
class를 위한 ApplicationContext를 로드하는 데 자동으로 사용되는 Config라는 이름의 static nested 설정 class를
선언합니다:
1@SpringJUnitConfig // (1) 2// ApplicationContext will be loaded from the static nested Config class 3class OrderServiceTest { 4 5 @Configuration 6 static class Config { 7 8 // this bean will be injected into the OrderServiceTest class 9 @Bean 10 OrderService orderService() { 11 OrderService orderService = new OrderServiceImpl(); 12 // set properties, etc. 13 return orderService; 14 } 15 } 16 17 @Autowired 18 OrderService orderService; 19 20 @Test 21 void testOrderService() { 22 // test the orderService 23 } 24 25}
| 1 | nested Config class에서 설정 정보를 로딩. |
1@SpringJUnitConfig // (1) 2// ApplicationContext will be loaded from the nested Config class 3class OrderServiceTest { 4 5 @Autowired 6 lateinit var orderService: OrderService 7 8 @Configuration 9 class Config { 10 11 // this bean will be injected into the OrderServiceTest class 12 @Bean 13 fun orderService(): OrderService { 14 // set properties, etc. 15 return OrderServiceImpl() 16 } 17 } 18 19 @Test 20 fun testOrderService() { 21 // test the orderService 22 } 23}
| 1 | nested Config class에서 설정 정보를 로딩. |
Context Configuration with Groovy Scripts
Mixing XML, Groovy Scripts, and Component Classes