Browse Source
This commit introduces an ApplicationContextFailureProcessor SPI in the Spring TestContext Framework that allows third parties to process failures that occur while a SmartContextLoader attempts to load an ApplicationContext. SmartContextLoader implementations must introduce a try-catch block around the loading code and throw a ContextLoadException that wraps the failed ApplicationContext and the cause of the failure. Extensions of AbstractTestContextBootstrapper can configure an ApplicationContextFailureProcessor by overriding the new protected getApplicationContextFailureProcessor() method. DefaultCacheAwareContextLoaderDelegate unwraps any ContextLoadException and delegates to the configured ApplicationContextFailureProcessor for processing. Closes gh-28826pull/29342/head
11 changed files with 391 additions and 53 deletions
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
|
||||
/** |
||||
* Strategy for components that process failures related to application contexts |
||||
* within the <em>Spring TestContext Framework</em>. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
* @see ContextLoadException |
||||
*/ |
||||
public interface ApplicationContextFailureProcessor { |
||||
|
||||
/** |
||||
* Invoked when a failure was encountered while attempting to load an |
||||
* {@link ApplicationContext}. |
||||
* <p>Implementations of this method must not throw any exceptions. Consequently, |
||||
* any exception thrown by an implementation of this method will be ignored. |
||||
* @param context the application context that did not load successfully |
||||
* @param exception the exception caught while loading the application context |
||||
*/ |
||||
void processLoadFailure(ApplicationContext context, Throwable exception); |
||||
|
||||
} |
||||
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
|
||||
/** |
||||
* Exception thrown when an error occurs while a {@link SmartContextLoader} |
||||
* attempts to load an {@link ApplicationContext}. |
||||
* |
||||
* <p>This exception provides access to the {@linkplain #getApplicationContext() |
||||
* application context} that failed to load as well as the {@linkplain #getCause() |
||||
* exception} caught while attempting to load that context. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
* @see SmartContextLoader#loadContext(MergedContextConfiguration) |
||||
*/ |
||||
@SuppressWarnings("serial") |
||||
public class ContextLoadException extends Exception { |
||||
|
||||
private final ApplicationContext applicationContext; |
||||
|
||||
|
||||
/** |
||||
* Create a new {@code ContextLoadException} for the supplied |
||||
* {@link ApplicationContext} and {@link Exception}. |
||||
* @param applicationContext the application context that failed to load |
||||
* @param cause the exception caught while attempting to load that context |
||||
*/ |
||||
public ContextLoadException(ApplicationContext applicationContext, Exception cause) { |
||||
super(cause); |
||||
this.applicationContext = applicationContext; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Get the {@code ApplicationContext} that failed to load. |
||||
* <p>Clients must not retain a long-lived reference to the context returned |
||||
* from this method. |
||||
*/ |
||||
public ApplicationContext getApplicationContext() { |
||||
return this.applicationContext; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,109 @@
@@ -0,0 +1,109 @@
|
||||
/* |
||||
* 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.List; |
||||
|
||||
import org.junit.jupiter.api.AfterEach; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.platform.testkit.engine.EngineTestKit; |
||||
|
||||
import org.springframework.beans.BeanInstantiationException; |
||||
import org.springframework.beans.factory.BeanCreationException; |
||||
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.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; |
||||
|
||||
/** |
||||
* Tests for failures that occur while loading an {@link ApplicationContext}. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
*/ |
||||
class ContextLoadFailureTests { |
||||
|
||||
static List<LoadFailure> loadFailures = new ArrayList<>(); |
||||
|
||||
|
||||
@BeforeEach |
||||
@AfterEach |
||||
void clearFailures() { |
||||
loadFailures.clear(); |
||||
} |
||||
|
||||
@Test |
||||
void customBootstrapperAppliesApplicationContextFailureProcessor() { |
||||
assertThat(loadFailures).isEmpty(); |
||||
|
||||
EngineTestKit.engine("junit-jupiter") |
||||
.selectors(selectClass(ExplosiveContextTestCase.class))//
|
||||
.execute() |
||||
.testEvents() |
||||
.assertStatistics(stats -> stats.started(1).succeeded(0).failed(1)); |
||||
|
||||
assertThat(loadFailures).hasSize(1); |
||||
LoadFailure loadFailure = loadFailures.get(0); |
||||
assertThat(loadFailure.context()).isExactlyInstanceOf(GenericApplicationContext.class); |
||||
assertThat(loadFailure.exception()) |
||||
.isInstanceOf(BeanCreationException.class) |
||||
.cause().isInstanceOf(BeanInstantiationException.class) |
||||
.rootCause().isInstanceOf(StackOverflowError.class).hasMessage("Boom!"); |
||||
} |
||||
|
||||
|
||||
@FailingTestCase |
||||
@SpringJUnitConfig |
||||
@BootstrapWith(CustomTestContextBootstrapper.class) |
||||
static class ExplosiveContextTestCase { |
||||
|
||||
@Test |
||||
void test1() { |
||||
/* no-op */ |
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
static class Config { |
||||
|
||||
@Bean |
||||
String explosion() { |
||||
throw new StackOverflowError("Boom!"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
static class CustomTestContextBootstrapper extends DefaultTestContextBootstrapper { |
||||
|
||||
@Override |
||||
protected ApplicationContextFailureProcessor getApplicationContextFailureProcessor() { |
||||
return (context, exception) -> loadFailures.add(new LoadFailure(context, exception)); |
||||
} |
||||
} |
||||
|
||||
record LoadFailure(ApplicationContext context, Throwable exception) {} |
||||
|
||||
} |
||||
Loading…
Reference in new issue