Loading...
Spring Framework Reference Documentation 7.0.2의 Context Configuration with Groovy Scripts의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
Groovy Bean Definition DSL을 사용하는 Groovy script를 사용하여 test에서 사용할 ApplicationContext를 로드하려면, test class에 @ContextConfiguration을 어노테이션으로 지정하고 locations 또는 value attribute를 Groovy script의 resource location이 포함된 array로 구성하면 됩니다. Groovy script에 대한 resource lookup semantics는 XML configuration files에 대해 설명된 것과 동일합니다.
Groovy script support 활성화<br>Spring TestContext Framework에서
ApplicationContext를 로드하기 위해 Groovy script를 사용하는 support는 Groovy가 classpath에 존재하면 자동으로 활성화됩니다.
다음 예는 Groovy configuration file을 지정하는 방법을 보여 줍니다:
1@ExtendWith(SpringExtension.class) 2// ApplicationContext will be loaded from "/AppConfig.groovy" and 3// "/TestConfig.groovy" in the root of the classpath 4@ContextConfiguration({"/AppConfig.groovy", "/TestConfig.Groovy"}) (1) 5class MyTest { 6 // class body... 7}
| 1 | Groovy configuration file의 location 지정. |
1@ExtendWith(SpringExtension::class) 2// ApplicationContext will be loaded from "/AppConfig.groovy" and 3// "/TestConfig.groovy" in the root of the classpath 4@ContextConfiguration("/AppConfig.groovy", "/TestConfig.Groovy") (1) 5class MyTest { 6 // class body... 7}
| 1 | Groovy configuration file의 location 지정. |
@ContextConfiguration 어노테이션에서 locations와 value attribute를 모두 생략하면, TestContext framework는 기본 Groovy script를 탐지하려고 시도합니다. 구체적으로, GenericGroovyXmlContextLoader와 GenericGroovyXmlWebContextLoader는 test class의 이름을 기반으로 기본 location을 탐지합니다.
class 이름이 com.example.MyTest이면, Groovy context loader는 "classpath:com/example/MyTestContext.groovy"에서 애플리케이션 컨텍스트를 로드합니다. 다음 예는 기본값을 사용하는 방법을 보여 줍니다:
1@ExtendWith(SpringExtension.class) 2// ApplicationContext will be loaded from 3// "classpath:com/example/MyTestContext.groovy" 4@ContextConfiguration (1) 5class MyTest { 6 // class body... 7}
| 1 | 기본 location에서 configuration 로드. |
1@ExtendWith(SpringExtension::class) 2// ApplicationContext will be loaded from 3// "classpath:com/example/MyTestContext.groovy" 4@ContextConfiguration (1) 5class MyTest { 6 // class body... 7}
| 1 | 기본 location에서 configuration 로드. |
XML configuration과 Groovy script를 동시에 선언<br>
@ContextConfiguration의locations또는valueattribute를 사용하여 XML configuration file과 Groovy script를 동시에 선언할 수 있습니다. 설정된 resource location의 path가.xml로 끝나면XmlBeanDefinitionReader를 사용하여 로드됩니다. 그렇지 않으면GroovyBeanDefinitionReader를 사용하여 로드됩니다.<br>다음 listing은 integration test에서 둘을 결합하는 방법을 보여 줍니다:<br><br>- Java<br><br>- Kotlin<br><br>java<br>@ExtendWith(SpringExtension.class)<br>// ApplicationContext will be loaded from<br>// "/app-config.xml" and "/TestConfig.groovy"<br>@ContextConfiguration({ "/app-config.xml", "/TestConfig.groovy" })<br>class MyTest {<br> // class body...<br>}<br><br><br>```kotlin<br>@ExtendWith(SpringExtension::class)<br>// ApplicationContext will be loaded from<br>// "/app-config.xml" and "/TestConfig.groovy"<br>@ContextConfiguration("/app-config.xml", "/TestConfig.groovy")<br>class MyTest {<br> // class body...<br>}<br>
Context Configuration with XML resources
Context Configuration with Component Classes