Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.springframework.test.context;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Indicates that the {@link org.springframework.context.ApplicationContext}
* associated with the test should be retained between test classes and
* not paused when it is reused from the context cache.
*
* <p>This is an opt-in mechanism intended for tests that rely on expensive
* {@link org.springframework.context.SmartLifecycle} components whose
* stop/start cycles significantly impact test performance.
*
* <p>When a test class is annotated with {@code @RetainApplicationContext},
* the application context will <em>not</em> be paused or restarted between
* test classes, even if the context would normally be considered unused.
*
* @since 7.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface RetainApplicationContext {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.style.DefaultToStringStyler;
import org.springframework.core.style.SimpleValueStyler;
import org.springframework.core.style.ToStringCreator;
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.MethodInvoker;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.*;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -145,16 +143,28 @@ public ApplicationContext getApplicationContext() {
* with this test context as <em>unused</em> so that it can be safely
* {@linkplain org.springframework.context.ConfigurableApplicationContext#pause() paused}
* if no other test classes are actively using the same application context.
*
* <p>When a test class is annotated with {@link RetainApplicationContext}, the
* application context will <em>not</em> be marked as unused and will be retained
* between test classes. This is intended for tests that rely on expensive
* {@link org.springframework.context.SmartLifecycle} components whose stop/start
* cycles significantly impact test performance.
*
* <p>The default implementation delegates to the {@link CacheAwareContextLoaderDelegate}
* that was supplied when this {@code TestContext} was constructed.
*
* @since 7.0
* @see CacheAwareContextLoaderDelegate#unregisterContextUsage(MergedContextConfiguration, Class)
* @see RetainApplicationContext
*/
@Override
public void markApplicationContextUnused() {
this.cacheAwareContextLoaderDelegate.unregisterContextUsage(this.mergedConfig, this.testClass);
if (!AnnotatedElementUtils.hasAnnotation(this.testClass, RetainApplicationContext.class)) {
this.cacheAwareContextLoaderDelegate.unregisterContextUsage(this.mergedConfig, this.testClass);
}
}


/**
* Mark the {@linkplain ApplicationContext application context} associated
* with this test context as <em>dirty</em> (i.e., by removing it from the
Expand Down