/** * Get the {@link ApplicationContext} associated with the supplied {@code ExtensionContext}. * @param context the current {@code ExtensionContext} (never {@code null}) * @return the application context * @throws IllegalStateException if an error occurs while retrieving the application context * @see org.springframework.test.context.TestContext#getApplicationContext() */ public static ApplicationContext getApplicationContext(ExtensionContext context) { return getTestContextManager(context).getTestContext().getApplicationContext(); }
BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
private ConfigurableApplicationContext getContext() { return (ConfigurableApplicationContext)getTestContext().getApplicationContext(); }
public static DataSource retrieveDataSource(TestContext testContext, @Nullable String name) { Assert.notNull(testContext, "TestContext must not be null"); BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
private ApplicationContext loadContext(Class<?> testClass) { TestContext testContext = TestContextTestUtils.buildTestContext(testClass, contextCache); return testContext.getApplicationContext(); }
public class WebContextTestExecutionListener extends AbstractTestExecutionListener { @Override public void prepareTestInstance(TestContext testContext) { if (testContext.getApplicationContext() instanceof GenericApplicationContext) { GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext(); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new SimpleThreadScope()); beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SimpleThreadScope()); } } }
public static PlatformTransactionManager retrieveTransactionManager(TestContext testContext, @Nullable String name) { Assert.notNull(testContext, "TestContext must not be null"); BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
/** * Performs dependency injection and bean initialization for the supplied * {@link TestContext} as described in * {@link #prepareTestInstance(TestContext) prepareTestInstance()}. * <p>The {@link #REINJECT_DEPENDENCIES_ATTRIBUTE} will be subsequently removed * from the test context, regardless of its value. * @param testContext the test context for which dependency injection should * be performed (never {@code null}) * @throws Exception allows any exception to propagate * @see #prepareTestInstance(TestContext) * @see #beforeTestMethod(TestContext) */ protected void injectDependencies(TestContext testContext) throws Exception { Object bean = testContext.getTestInstance(); Class<?> clazz = testContext.getTestClass(); AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory(); beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false); beanFactory.initializeBean(bean, clazz.getName() + AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX); testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE); }
@Override public void afterTestClass(TestContext testContext) throws Exception { RepositoryService repositoryService = testContext.getApplicationContext().getBean(RepositoryService.class); for (Deployment deployment : repositoryService.createDeploymentQuery().list()) { repositoryService.deleteDeployment(deployment.getId(), true); } }
ApplicationContext context = testContext.getApplicationContext();
@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); }
@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); }
@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); }
@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); }
@Test public void missingDataSourceAndTxMgr() throws Exception { ApplicationContext ctx = mock(ApplicationContext.class); given(ctx.getResource(anyString())).willReturn(mock(Resource.class)); given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class)); Class<?> clazz = MissingDataSourceAndTxMgr.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); given(testContext.getApplicationContext()).willReturn(ctx); assertExceptionContains("supply at least a DataSource or PlatformTransactionManager"); }
@Test public void isolatedTxModeDeclaredWithoutTxMgr() throws Exception { ApplicationContext ctx = mock(ApplicationContext.class); given(ctx.getResource(anyString())).willReturn(mock(Resource.class)); given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class)); Class<?> clazz = IsolatedWithoutTxMgr.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); given(testContext.getApplicationContext()).willReturn(ctx); assertExceptionContains("cannot execute SQL scripts using Transaction Mode [ISOLATED] without a PlatformTransactionManager"); }
@Test public void standardApplicationContext() throws Exception { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(getClass()); given(testContext.getApplicationContext()).willReturn(mock(ApplicationContext.class)); listener.beforeTestClass(testContext); assertSetUpOutsideOfStelAttributeExists(); listener.prepareTestInstance(testContext); assertSetUpOutsideOfStelAttributeExists(); listener.beforeTestMethod(testContext); assertSetUpOutsideOfStelAttributeExists(); listener.afterTestMethod(testContext); assertSetUpOutsideOfStelAttributeExists(); }
@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); }
@Test public void removeContextHierarchyCacheLevel3Then2WithExhaustiveMode() { // 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 // Should wipe the cache. contextCache.remove(getMergedContextConfiguration(testContext3a), HierarchyMode.EXHAUSTIVE); assertContextCacheStatistics(contextCache, "removed level 3-A", 0, 1, 4); assertParentContextCount(0); // Remove Level 2 // Should not actually do anything since the cache was cleared in the // previous step. So the stats should remain the same. contextCache.remove(getMergedContextConfiguration(testContext3b).getParent(), HierarchyMode.EXHAUSTIVE); assertContextCacheStatistics(contextCache, "removed level 2", 0, 1, 4); assertParentContextCount(0); }
@Before public void setUp() { given(wac.getServletContext()).willReturn(mockServletContext); given(testContext.getApplicationContext()).willReturn(wac); MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext); MockHttpServletResponse response = new MockHttpServletResponse(); ServletWebRequest servletWebRequest = new ServletWebRequest(request, response); request.setAttribute(SET_UP_OUTSIDE_OF_STEL, "true"); RequestContextHolder.setRequestAttributes(servletWebRequest); assertSetUpOutsideOfStelAttributeExists(); }