Add integration tests for context failure threshold support
See gh-14182
This commit is contained in:
parent
52ae97cffc
commit
56f3bd86cf
|
@ -16,30 +16,126 @@
|
||||||
|
|
||||||
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 {
|
||||||
|
|
||||||
|
private static final AtomicInteger loadCount = new AtomicInteger(0);
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
@AfterEach
|
||||||
|
void resetFlag() {
|
||||||
|
loadCount.set(0);
|
||||||
|
SpringProperties.setProperty(CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void defaultThreshold() {
|
||||||
|
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
|
||||||
|
void customThreshold() {
|
||||||
|
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
|
||||||
|
void thresholdEffectivelyDisabled() {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SpringJUnitConfig
|
||||||
|
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
|
||||||
|
static class PassingTestCase {
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void verifyInitialCacheState() {
|
||||||
|
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
|
@BeforeAll
|
||||||
static void verifyInitialCacheState() {
|
static void verifyInitialCacheState() {
|
||||||
resetContextCache();
|
resetContextCache();
|
||||||
|
@ -53,28 +149,26 @@ class ContextFailureThresholdTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(1)
|
void test1() {}
|
||||||
void test1() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(2)
|
void test2() {}
|
||||||
void test2() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(3)
|
void test3() {}
|
||||||
void test3() {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
static class FailingConfig {
|
static class FailingConfig {
|
||||||
|
|
||||||
|
FailingConfig() {
|
||||||
|
loadCount.incrementAndGet();
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
String explosiveString() {
|
String explosiveString() {
|
||||||
throw new RuntimeException("Boom!");
|
throw new RuntimeException("Boom!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue