Loading...
Spring Framework Reference Documentation 7.0.2의 @DirtiesContext의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
@DirtiesContext@DirtiesContext는 test를 실행하는 동안 (즉, test가 어떤 방식으로든 이를 변경하거나 손상시켜서 — 예를 들어, singleton bean의 state를 변경함으로써) 기반이 되는 Spring ApplicationContext가 dirty 상태가 되었으며 닫혀야 함을 나타냅니다. 애플리케이션 context가 dirty로 표시되면, 테스트 프레임워크의 캐시에서 제거되고 닫힙니다.
그 결과, 동일한 설정 메타데이터를 가진 context가 필요한 이후의 test에 대해서는 기반이 되는 Spring 컨테이너가 다시 생성됩니다.
@DirtiesContext는 동일한 test 클래스나 test 클래스 계층 내에서 클래스 레벨과 메서드 레벨 어노테이션 모두로 사용할 수 있습니다. 이러한 상황에서 ApplicationContext는 설정된 methodMode와 classMode에 따라 해당 어노테이션이 달린 메서드 전후뿐만 아니라 현재 test 클래스 전후에도 dirty로 표시됩니다.
@DirtiesContext가 클래스 레벨과 메서드 레벨 모두에 선언된 경우, 두 어노테이션에서 설정된 mode가 모두 적용됩니다. 예를 들어, class mode가 BEFORE_EACH_TEST_METHOD로 설정되고 method mode가 AFTER_METHOD로 설정된 경우, context는 주어진 test 메서드 전과 후에 모두 dirty로 표시됩니다.
다음 예시는 다양한 설정 시나리오에서 context가 언제 dirty로 표시되는지를 설명합니다:
BEFORE_CLASS로 설정된 클래스에 선언된 경우, 현재 test 클래스 이전에.1@DirtiesContext(classMode = BEFORE_CLASS) (1) 2class FreshContextTests { 3 // some tests that require a new Spring container 4}
| 1 | Dirty the context before the current test class. |
1@DirtiesContext(classMode = BEFORE_CLASS) (1) 2class FreshContextTests { 3 // some tests that require a new Spring container 4}
| 1 | Dirty the context before the current test class. |
AFTER_CLASS(즉, 기본 class mode)로 설정된 클래스에 선언된 경우, 현재 test 클래스 이후에.1@DirtiesContext (1) 2class ContextDirtyingTests { 3 // some tests that result in the Spring container being dirtied 4}
| 1 | Dirty the context after the current test class. |
1@DirtiesContext (1) 2class ContextDirtyingTests { 3 // some tests that result in the Spring container being dirtied 4}
| 1 | Dirty the context after the current test class. |
BEFORE_EACH_TEST_METHOD.로 설정된 클래스에 선언된 경우, 현재 test 클래스의 각 test 메서드 이전에.1@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) (1) 2class FreshContextTests { 3 // some tests that require a new Spring container 4}
| 1 | Dirty the context before each test method. |
1@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) (1) 2class FreshContextTests { 3 // some tests that require a new Spring container 4}
| 1 | Dirty the context before each test method. |
AFTER_EACH_TEST_METHOD.로 설정된 클래스에 선언된 경우, 현재 test 클래스의 각 test 메서드 이후에.1@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) (1) 2class ContextDirtyingTests { 3 // some tests that result in the Spring container being dirtied 4}
| 1 | Dirty the context after each test method. |
1@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) (1) 2class ContextDirtyingTests { 3 // some tests that result in the Spring container being dirtied 4}
| 1 | Dirty the context after each test method. |
BEFORE_METHOD로 설정된 메서드에 선언된 경우, 현재 test 이전에.1@DirtiesContext(methodMode = BEFORE_METHOD) (1) 2@Test 3void testProcessWhichRequiresFreshAppCtx() { 4 // some logic that requires a new Spring container 5}
| 1 | Dirty the context before the current test method. |
1@DirtiesContext(methodMode = BEFORE_METHOD) (1) 2@Test 3fun testProcessWhichRequiresFreshAppCtx() { 4 // some logic that requires a new Spring container 5}
| 1 | Dirty the context before the current test method. |
AFTER_METHOD(즉, 기본 method mode)로 설정된 메서드에 선언된 경우, 현재 test 이후에.1@DirtiesContext (1) 2@Test 3void testProcessWhichDirtiesAppCtx() { 4 // some logic that results in the Spring container being dirtied 5}
| 1 | Dirty the context after the current test method. |
1@DirtiesContext (1) 2@Test 3fun testProcessWhichDirtiesAppCtx() { 4 // some logic that results in the Spring container being dirtied 5}
| 1 | Dirty the context after the current test method. |
context가 @ContextHierarchy를 사용한 context 계층의 일부로 구성된 test에서 @DirtiesContext를 사용하는 경우, hierarchyMode 플래그를 사용하여 context 캐시가 어떻게 clear될지를 제어할 수 있습니다. 기본적으로는, 현재 레벨뿐만 아니라 현재 test와 공통의 ancestor context를 공유하는 다른 모든 context 계층도 포함하여 context 캐시를 clear하기 위해 exhaustive 알고리즘이 사용됩니다.
공통 ancestor context의 하위 계층에 위치한 모든 ApplicationContext 인스턴스는 context 캐시에서 제거되고 닫힙니다. exhaustive 알고리즘이 특정 use case에 과도하다면, 다음 예제에서 보이는 것처럼 더 단순한 current level 알고리즘을 지정할 수 있습니다.
1@ContextHierarchy({ 2 @ContextConfiguration("/parent-config.xml"), 3 @ContextConfiguration("/child-config.xml") 4}) 5class BaseTests { 6 // class body... 7} 8 9class ExtendedTests extends BaseTests { 10 11 @Test 12 @DirtiesContext(hierarchyMode = CURRENT_LEVEL) (1) 13 void test() { 14 // some logic that results in the child context being dirtied 15 } 16}
| 1 | Use the current-level algorithm. |
1@ContextHierarchy( 2 ContextConfiguration("/parent-config.xml"), 3 ContextConfiguration("/child-config.xml")) 4open class BaseTests { 5 // class body... 6} 7 8class ExtendedTests : BaseTests() { 9 10 @Test 11 @DirtiesContext(hierarchyMode = CURRENT_LEVEL) (1) 12 fun test() { 13 // some logic that results in the child context being dirtied 14 } 15}
| 1 | Use the current-level algorithm. |
EXHAUSTIVE와 CURRENT_LEVEL 알고리즘에 대한 자세한 내용은
DirtiesContext.HierarchyMode
javadoc을 참조하십시오.
@MockitoBean and @MockitoSpyBean
@RecordApplicationEvents