Add integration tests for context failure threshold support

See gh-14182
This commit is contained in:
Sam Brannen 2023-06-09 14:39:41 +02:00
parent 52ae97cffc
commit 56f3bd86cf
1 changed files with 119 additions and 25 deletions

View File

@ -16,64 +16,158 @@
package org.springframework.test.context.cache; package org.springframework.test.context.cache;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.TestMethodOrder;
import org.junit.platform.testkit.engine.EngineTestKit;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.SpringProperties;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.springframework.test.context.CacheAwareContextLoaderDelegate.CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME;
import static org.springframework.test.context.CacheAwareContextLoaderDelegate.DEFAULT_CONTEXT_FAILURE_THRESHOLD;
import static org.springframework.test.context.cache.ContextCacheTestUtils.assertContextCacheStatistics; import static org.springframework.test.context.cache.ContextCacheTestUtils.assertContextCacheStatistics;
import static org.springframework.test.context.cache.ContextCacheTestUtils.resetContextCache; import static org.springframework.test.context.cache.ContextCacheTestUtils.resetContextCache;
/** /**
* Integration tests for context failure threshold support.
*
* @author Sam Brannen * @author Sam Brannen
* @since 6.1 * @since 6.1
*/ */
@SpringJUnitConfig
@TestMethodOrder(OrderAnnotation.class)
@Disabled
class ContextFailureThresholdTests { class ContextFailureThresholdTests {
@BeforeAll private static final AtomicInteger loadCount = new AtomicInteger(0);
static void verifyInitialCacheState() {
resetContextCache();
assertContextCacheStatistics("BeforeAll", 0, 0, 0);
}
@AfterAll
static void verifyFinalCacheState() { @BeforeEach
assertContextCacheStatistics("AfterAll", 0, 0, 3); @AfterEach
resetContextCache(); void resetFlag() {
loadCount.set(0);
SpringProperties.setProperty(CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME, null);
} }
@Test @Test
@Order(1) void defaultThreshold() {
void test1() { assertThat(loadCount.get()).isZero();
EngineTestKit.engine("junit-jupiter")//
.selectors(selectClass(PassingTestCase.class))// 2 passing
.selectors(selectClass(FailingTestCase.class))// 3 failing
.execute()//
.testEvents()//
.assertStatistics(stats -> stats.started(5).succeeded(2).failed(3));
assertThat(loadCount.get()).isEqualTo(DEFAULT_CONTEXT_FAILURE_THRESHOLD);
} }
@Test @Test
@Order(2) void customThreshold() {
void test2() { assertThat(loadCount.get()).isZero();
int threshold = 2;
SpringProperties.setProperty(CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME, Integer.toString(threshold));
EngineTestKit.engine("junit-jupiter")//
.selectors(selectClass(PassingTestCase.class))// 2 passing
.selectors(selectClass(FailingTestCase.class))// 3 failing
.execute()//
.testEvents()//
.assertStatistics(stats -> stats.started(5).succeeded(2).failed(3));
assertThat(loadCount.get()).isEqualTo(threshold);
} }
@Test @Test
@Order(3) void thresholdEffectivelyDisabled() {
void test3() { assertThat(loadCount.get()).isZero();
SpringProperties.setProperty(CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME, "999999");
EngineTestKit.engine("junit-jupiter")//
.selectors(selectClass(PassingTestCase.class))// 2 passing
.selectors(selectClass(FailingTestCase.class))// 3 failing
.execute()//
.testEvents()//
.assertStatistics(stats -> stats.started(5).succeeded(2).failed(3));
assertThat(loadCount.get()).isEqualTo(3);
} }
@Configuration @SpringJUnitConfig
static class FailingConfig { @TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
static class PassingTestCase {
@Bean @BeforeAll
String explosiveString() { static void verifyInitialCacheState() {
throw new RuntimeException("Boom!"); resetContextCache();
assertContextCacheStatistics("BeforeAll", 0, 0, 0);
}
@AfterAll
static void verifyFinalCacheState() {
assertContextCacheStatistics("AfterAll", 1, 1, 1);
resetContextCache();
}
@Test
void test1() {}
@Test
void test2() {}
@Configuration
static class PassingConfig {
}
}
@SpringJUnitConfig
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
@TestMethodOrder(OrderAnnotation.class)
static class FailingTestCase {
@BeforeAll
static void verifyInitialCacheState() {
resetContextCache();
assertContextCacheStatistics("BeforeAll", 0, 0, 0);
}
@AfterAll
static void verifyFinalCacheState() {
assertContextCacheStatistics("AfterAll", 0, 0, 3);
resetContextCache();
}
@Test
void test1() {}
@Test
void test2() {}
@Test
void test3() {}
@Configuration
static class FailingConfig {
FailingConfig() {
loadCount.incrementAndGet();
}
@Bean
String explosiveString() {
throw new RuntimeException("Boom!");
}
} }
} }