Search in sources :

Example 6 with TestContext

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);
}
Also used : TestContext(org.springframework.test.context.TestContext) Test(org.junit.Test)

Example 7 with TestContext

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);
}
Also used : TestContext(org.springframework.test.context.TestContext) Test(org.junit.Test)

Example 8 with TestContext

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"));
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) SpringApplication(org.springframework.boot.SpringApplication) TestContext(org.springframework.test.context.TestContext) Test(org.junit.Test)

Example 9 with TestContext

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;
}
Also used : TestContext(org.springframework.test.context.TestContext)

Example 10 with 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;
}
Also used : WebEnvironment(org.springframework.boot.test.context.SpringBootTest.WebEnvironment) TestContext(org.springframework.test.context.TestContext)

Aggregations

TestContext (org.springframework.test.context.TestContext)11 Test (org.junit.Test)8 Method (java.lang.reflect.Method)1 SpringApplication (org.springframework.boot.SpringApplication)1 WebEnvironment (org.springframework.boot.test.context.SpringBootTest.WebEnvironment)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 MergedContextConfiguration (org.springframework.test.context.MergedContextConfiguration)1