use of org.springframework.test.context.TestContext in project spring-framework by spring-projects.
the class ContextCacheTests method removeContextHierarchyCacheLevel1WithExhaustiveMode.
@Test
public void removeContextHierarchyCacheLevel1WithExhaustiveMode() {
// Load Level 3-A
TestContext testContext3a = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 1
// Should also remove Levels 2, 3-A, and 3-B, leaving nothing.
contextCache.remove(getMergedContextConfiguration(testContext3a).getParent().getParent(), HierarchyMode.EXHAUSTIVE);
assertContextCacheStatistics(contextCache, "removed level 1", 0, 1, 4);
assertParentContextCount(0);
}
use of org.springframework.test.context.TestContext in project spring-framework by spring-projects.
the class ContextCacheTests method removeContextHierarchyCacheLevel2WithExhaustiveMode.
@Test
public void removeContextHierarchyCacheLevel2WithExhaustiveMode() {
// Load Level 3-A
TestContext testContext3a = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 2
// Should wipe the cache
contextCache.remove(getMergedContextConfiguration(testContext3a).getParent(), HierarchyMode.EXHAUSTIVE);
assertContextCacheStatistics(contextCache, "removed level 2", 0, 1, 4);
assertParentContextCount(0);
}
use of org.springframework.test.context.TestContext in project spring-boot by spring-projects.
the class SpringBootDependencyInjectionTestExecutionListenerTests method prepareFailingTestInstanceShouldPrintReport.
@Test
public void prepareFailingTestInstanceShouldPrintReport() throws Exception {
TestContext testContext = mock(TestContext.class);
given(testContext.getTestInstance()).willThrow(new IllegalStateException());
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
ConfigurableApplicationContext applicationContext = application.run();
given(testContext.getApplicationContext()).willReturn(applicationContext);
try {
this.reportListener.prepareTestInstance(testContext);
} catch (IllegalStateException ex) {
// Expected
}
this.out.expect(containsString("AUTO-CONFIGURATION REPORT"));
this.out.expect(containsString("Positive matches"));
this.out.expect(containsString("Negative matches"));
}
use of org.springframework.test.context.TestContext in project spring-boot by spring-projects.
the class MockitoTestExecutionListenerTests method mockTestContext.
@SuppressWarnings({ "unchecked", "rawtypes" })
private TestContext mockTestContext(Object instance) {
TestContext testContext = mock(TestContext.class);
given(testContext.getTestInstance()).willReturn(instance);
given(testContext.getTestClass()).willReturn((Class) instance.getClass());
given(testContext.getApplicationContext()).willReturn(this.applicationContext);
return testContext;
}
use of org.springframework.test.context.TestContext in project spring-boot by spring-projects.
the class SpringBootTestContextBootstrapper method buildTestContext.
@Override
public TestContext buildTestContext() {
TestContext context = super.buildTestContext();
verifyConfiguration(context.getTestClass());
WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass());
if (webEnvironment == WebEnvironment.MOCK && deduceWebApplication() == WebApplicationType.SERVLET) {
context.setAttribute(ACTIVATE_SERVLET_LISTENER, true);
} else if (webEnvironment != null && webEnvironment.isEmbedded()) {
context.setAttribute(ACTIVATE_SERVLET_LISTENER, false);
}
return context;
}
Aggregations