Load ApplicationContextFailureProcessors from spring.factories

At the request of the Spring Boot team, ApplicationContextFailureProcessor
implementations are now loaded via the spring.factories mechanism
instead of supporting a single processor registered via subclasses of
AbstractTestContextBootstrapper.

This makes the retrieval and handling of processors internal to
DefaultCacheAwareContextLoaderDelegate, while simultaneously supporting
multiple processors that can be registered by anyone (i.e., not
limited to projects that implement custom TestContextBootstrappers).

See gh-28826
Closes gh-29387
This commit is contained in:
Sam Brannen
2022-10-26 19:00:27 +02:00
parent 273e38c2b4
commit a13cb01b99
6 changed files with 141 additions and 75 deletions

View File

@@ -16,9 +16,6 @@
package org.springframework.test.context.failures;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -30,11 +27,9 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.ApplicationContextFailureProcessor;
import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.failures.TrackingApplicationContextFailureProcessor.LoadFailure;
import org.springframework.test.context.junit.jupiter.FailingTestCase;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.DefaultTestContextBootstrapper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
@@ -47,18 +42,15 @@ import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass
*/
class ContextLoadFailureTests {
static List<LoadFailure> loadFailures = new ArrayList<>();
@BeforeEach
@AfterEach
void clearFailures() {
loadFailures.clear();
TrackingApplicationContextFailureProcessor.loadFailures.clear();
}
@Test
void customBootstrapperAppliesApplicationContextFailureProcessor() {
assertThat(loadFailures).isEmpty();
assertThat(TrackingApplicationContextFailureProcessor.loadFailures).isEmpty();
EngineTestKit.engine("junit-jupiter")
.selectors(selectClass(ExplosiveContextTestCase.class))//
@@ -66,8 +58,8 @@ class ContextLoadFailureTests {
.testEvents()
.assertStatistics(stats -> stats.started(1).succeeded(0).failed(1));
assertThat(loadFailures).hasSize(1);
LoadFailure loadFailure = loadFailures.get(0);
assertThat(TrackingApplicationContextFailureProcessor.loadFailures).hasSize(1);
LoadFailure loadFailure = TrackingApplicationContextFailureProcessor.loadFailures.get(0);
assertThat(loadFailure.context()).isExactlyInstanceOf(GenericApplicationContext.class);
assertThat(loadFailure.exception())
.isInstanceOf(BeanCreationException.class)
@@ -78,7 +70,6 @@ class ContextLoadFailureTests {
@FailingTestCase
@SpringJUnitConfig
@BootstrapWith(CustomTestContextBootstrapper.class)
static class ExplosiveContextTestCase {
@Test
@@ -96,14 +87,4 @@ class ContextLoadFailureTests {
}
}
static class CustomTestContextBootstrapper extends DefaultTestContextBootstrapper {
@Override
protected ApplicationContextFailureProcessor getApplicationContextFailureProcessor() {
return (context, exception) -> loadFailures.add(new LoadFailure(context, exception));
}
}
record LoadFailure(ApplicationContext context, Throwable exception) {}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.failures;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ApplicationContextFailureProcessor;
/**
* Demo {@link ApplicationContextFailureProcessor} for tests that tracks
* {@linkplain LoadFailure load failures} that can be queried via
* {@link #loadFailures}.
*
* @author Sam Brannen
* @since 6.0
*/
public class TrackingApplicationContextFailureProcessor implements ApplicationContextFailureProcessor {
public static List<LoadFailure> loadFailures = Collections.synchronizedList(new ArrayList<>());
@Override
public void processLoadFailure(ApplicationContext context, Throwable exception) {
loadFailures.add(new LoadFailure(context, exception));
}
public record LoadFailure(ApplicationContext context, Throwable exception) {}
}