org.springframework.test.context.TestContext#markApplicationContextDirty ( )源码实例Demo

下面列出了org.springframework.test.context.TestContext#markApplicationContextDirty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public void afterTestClass(TestContext testContext) {
	if (applicationContextBroken(testContext)
			|| wireMockConfigurationMissing(testContext)
			|| annotationMissing(testContext)) {
		return;
	}
	if (portIsFixed(testContext)) {
		if (log.isWarnEnabled()) {
			log.warn("You've used fixed ports for WireMock setup - "
					+ "will mark context as dirty. Please use random ports, as much "
					+ "as possible. Your tests will be faster and more reliable and this "
					+ "warning will go away");
		}
		testContext
				.markApplicationContextDirty(DirtiesContext.HierarchyMode.EXHAUSTIVE);
	}
	else {
		if (log.isDebugEnabled()) {
			log.debug(
					"Resetting mappings for the next test to restart them. That's necessary when"
							+ " reusing the same context with new servers running on random ports");
		}
		wireMockConfig(testContext).reRegisterServerWithResetMappings();
	}
}
 
@Override
public void afterTestClass(TestContext testContext) {
	if (testContext.getTestClass()
			.getAnnotationsByType(AutoConfigureStubRunner.class).length == 0) {
		if (log.isDebugEnabled()) {
			log.debug("No @AutoConfigureStubRunner annotation found on ["
					+ testContext.getTestClass() + "]. Skipping");
		}
		return;
	}
	if (!WireMockHttpServerStub.SERVERS.isEmpty() && WireMockHttpServerStub.SERVERS
			.values().stream().noneMatch(p -> p.random)) {
		if (log.isWarnEnabled()) {
			log.warn("You've used fixed ports for WireMock setup - "
					+ "will mark context as dirty. Please use random ports, as much "
					+ "as possible. Your tests will be faster and more reliable and this "
					+ "warning will go away");
		}
		testContext
				.markApplicationContextDirty(DirtiesContext.HierarchyMode.EXHAUSTIVE);
	}
	// potential race condition
	WireMockHttpServerStub.SERVERS.clear();
}
 
/**
 * Mark the {@linkplain ApplicationContext application context} of the supplied
 * {@linkplain TestContext test context} as
 * {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty}
 * and set {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
 * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}.
 * @param testContext the test context whose application context should
 * be marked as dirty
 * @param hierarchyMode the context cache clearing mode to be applied if the
 * context is part of a hierarchy; may be {@code null}
 * @since 3.2.2
 */
protected void dirtyContext(TestContext testContext, @Nullable HierarchyMode hierarchyMode) {
	testContext.markApplicationContextDirty(hierarchyMode);
	testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}
 
/**
 * Mark the {@linkplain ApplicationContext application context} of the supplied
 * {@linkplain TestContext test context} as
 * {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty}
 * and set {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
 * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}.
 * @param testContext the test context whose application context should
 * be marked as dirty
 * @param hierarchyMode the context cache clearing mode to be applied if the
 * context is part of a hierarchy; may be {@code null}
 * @since 3.2.2
 */
protected void dirtyContext(TestContext testContext, @Nullable HierarchyMode hierarchyMode) {
	testContext.markApplicationContextDirty(hierarchyMode);
	testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}
 
/**
 * Mark the {@linkplain ApplicationContext application context} of the supplied
 * {@linkplain TestContext test context} as
 * {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty}
 * and set {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
 * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}.
 * @param testContext the test context whose application context should
 * be marked as dirty
 * @param hierarchyMode the context cache clearing mode to be applied if the
 * context is part of a hierarchy; may be {@code null}
 * @since 3.2.2
 */
protected void dirtyContext(TestContext testContext, HierarchyMode hierarchyMode) {
	testContext.markApplicationContextDirty(hierarchyMode);
	testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}
 
/**
 * Marks the {@link ApplicationContext application context} of the supplied {@link TestContext test context} as
 * {@link TestContext#markApplicationContextDirty() dirty}, and sets the
 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE REINJECT_DEPENDENCIES_ATTRIBUTE}
 * in the test context to <code>true</code> .
 */
protected void reloadContext(TestContext testContext) {
	testContext.markApplicationContextDirty();
	testContext
			.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}