Search in sources :

Example 1 with TestContext

use of org.springframework.test.context.TestContext in project spring-boot by spring-projects.

the class SpringBootContextLoaderTests method getEnvironmentProperties.

private Map<String, Object> getEnvironmentProperties(Class<?> testClass) throws Exception {
    TestContext context = new ExposedTestContextManager(testClass).getExposedTestContext();
    MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context, "mergedContextConfiguration");
    return TestPropertySourceUtils.convertInlinedPropertiesToMap(config.getPropertySourceProperties());
}
Also used : MergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration) TestContext(org.springframework.test.context.TestContext)

Example 2 with TestContext

use of org.springframework.test.context.TestContext in project spring-framework by spring-projects.

the class ContextCacheTests method removeContextHierarchyCacheLevel2.

@Test
public void removeContextHierarchyCacheLevel2() {
    // 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 also remove Levels 3-A and 3-B, leaving only Level 1 as a context in the
    // cache but also removing the Level 1 hierarchy since all children have been
    // removed.
    contextCache.remove(getMergedContextConfiguration(testContext3a).getParent(), HierarchyMode.CURRENT_LEVEL);
    assertContextCacheStatistics(contextCache, "removed level 2", 1, 1, 4);
    assertParentContextCount(0);
}
Also used : TestContext(org.springframework.test.context.TestContext) Test(org.junit.Test)

Example 3 with TestContext

use of org.springframework.test.context.TestContext in project spring-framework by spring-projects.

the class ContextCacheTests method removeContextHierarchyCacheLevel3Then2.

@Test
public void removeContextHierarchyCacheLevel3Then2() {
    // 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 3-A
    contextCache.remove(getMergedContextConfiguration(testContext3a), HierarchyMode.CURRENT_LEVEL);
    assertContextCacheStatistics(contextCache, "removed level 3-A", 3, 1, 4);
    assertParentContextCount(2);
    // Remove Level 2
    // Should also remove Level 3-B, leaving only Level 1.
    contextCache.remove(getMergedContextConfiguration(testContext3b).getParent(), HierarchyMode.CURRENT_LEVEL);
    assertContextCacheStatistics(contextCache, "removed level 2", 1, 1, 4);
    assertParentContextCount(0);
}
Also used : TestContext(org.springframework.test.context.TestContext) Test(org.junit.Test)

Example 4 with TestContext

use of org.springframework.test.context.TestContext in project spring-framework by spring-projects.

the class ContextCacheTests method removeContextHierarchyCacheLevel1.

@Test
public void removeContextHierarchyCacheLevel1() {
    // 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.CURRENT_LEVEL);
    assertContextCacheStatistics(contextCache, "removed level 1", 0, 1, 4);
    assertParentContextCount(0);
}
Also used : TestContext(org.springframework.test.context.TestContext) Test(org.junit.Test)

Example 5 with TestContext

use of org.springframework.test.context.TestContext in project spring-security by spring-projects.

the class WithSecurityContextTestExcecutionListenerTests method handlesGenericAnnotation.

@Test
public // gh-3837
void handlesGenericAnnotation() throws Exception {
    Method method = ReflectionUtils.findMethod(WithSecurityContextTestExcecutionListenerTests.class, "handlesGenericAnnotationTestMethod");
    TestContext testContext = mock(TestContext.class);
    when(testContext.getTestMethod()).thenReturn(method);
    when(testContext.getApplicationContext()).thenThrow(new IllegalStateException(""));
    this.listener.beforeTestMethod(testContext);
    assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal()).isInstanceOf(WithSuperClassWithSecurityContext.class);
}
Also used : TestContext(org.springframework.test.context.TestContext) Method(java.lang.reflect.Method) Test(org.junit.Test)

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