Loading...
Spring Framework Reference Documentation 7.0.2의 Context Management의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
각 TestContext는 자신이 책임지는 test 인스턴스에 대한 컨텍스트 관리와 캐싱 지원을 제공합니다. Test 인스턴스는 자동으로 설정된 ApplicationContext에 대한 액세스를 받지 않습니다. 그러나 test 클래스가 ApplicationContextAware 인터페이스를 구현하면 ApplicationContext에 대한 레퍼런스가 test 인스턴스에 제공됩니다.
AbstractJUnit4SpringContextTests와 AbstractTestNGSpringContextTests가 ApplicationContextAware를 구현하므로 자동으로 ApplicationContext에 대한 액세스를 제공한다는 점에 유의하십시오.
@Autowired ApplicationContext
ApplicationContextAware 인터페이스를 구현하는 대신, 다음 예제에서 보듯이 필드나 setter 메서드에 @Autowired 어노테이션을 사용하여 test 클래스에 애플리케이션 컨텍스트를 주입할 수 있습니다:
1@SpringJUnitConfig 2class MyTest { 3 4 @Autowired // (1) 5 ApplicationContext applicationContext; 6 7 // class body... 8}
| 1 | ApplicationContext 주입. |
1@SpringJUnitConfig 2class MyTest { 3 4 @Autowired // (1) 5 lateinit var applicationContext: ApplicationContext 6 7 // class body... 8}
| 1 | ApplicationContext 주입. |
마찬가지로, test가 WebApplicationContext를 로드하도록 설정된 경우 다음과 같이 웹 애플리케이션 컨텍스트를 test에 주입할 수 있습니다:
1@SpringJUnitWebConfig // (1) 2class MyWebAppTest { 3 4 @Autowired // (2) 5 WebApplicationContext wac; 6 7 // class body... 8}
| 1 | WebApplicationContext 설정. |
| 2 | WebApplicationContext 주입. |
1@SpringJUnitWebConfig // (1) 2class MyWebAppTest { 3 4 @Autowired // (2) 5 lateinit var wac: WebApplicationContext 6 7 // class body... 8}
| 1 | WebApplicationContext 설정. |
| 2 | WebApplicationContext 주입. |
@Autowired를 사용하는 의존성 주입은 기본적으로 설정되어 있는 DependencyInjectionTestExecutionListener에 의해 제공됩니다 (Dependency Injection of Test Fixtures 참조).
TestContext 프레임워크를 사용하는 test 클래스는 애플리케이션 컨텍스트를 설정하기 위해 특정 클래스를 상속하거나 특정 인터페이스를 구현할 필요가 없습니다. 대신 클래스 레벨에서 @ContextConfiguration 어노테이션을 선언함으로써 설정을 수행합니다. Test 클래스가 애플리케이션 컨텍스트 리소스 위치나 컴포넌트 클래스를 명시적으로 선언하지 않으면, 설정된 ContextLoader가 기본 위치나 기본 설정 클래스에서 컨텍스트를 로드하는 방법을 결정합니다.
컨텍스트 리소스 위치와 컴포넌트 클래스에 더해, 애플리케이션 컨텍스트는 애플리케이션 컨텍스트 이니셜라이저를 통해서도 설정할 수 있습니다.
다음 섹션에서는 Spring의 @ContextConfiguration 어노테이션을 사용하여 XML 설정 파일, Groovy 스크립트, 컴포넌트 클래스(일반적으로 @Configuration 클래스), 또는 컨텍스트 이니셜라이저를 사용해 test ApplicationContext를 설정하는 방법을 설명합니다. 또는 고급 사용 사례를 위해 커스텀 SmartContextLoader를 직접 구현하고 설정할 수도 있습니다.
WebApplicationContextTest Execution Events
Context Configuration with XML resources