org.springframework.test.context.TestContext.getApplicationContext()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(13.3k)|赞(0)|评价(0)|浏览(104)

本文整理了Java中org.springframework.test.context.TestContext.getApplicationContext()方法的一些代码示例,展示了TestContext.getApplicationContext()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TestContext.getApplicationContext()方法的具体详情如下:
包路径:org.springframework.test.context.TestContext
类名称:TestContext
方法名:getApplicationContext

TestContext.getApplicationContext介绍

[英]Get the ApplicationContext for this test context, possibly cached.

Implementations of this method are responsible for loading the application context if the corresponding context has not already been loaded, potentially caching the context as well.
[中]获取此测试上下文的ApplicationContext,可能是缓存的。
如果尚未加载相应的上下文,则此方法的实现负责加载应用程序上下文,可能还会缓存上下文。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * 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();
}

代码示例来源:origin: spring-projects/spring-framework

BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();

代码示例来源:origin: cucumber/cucumber-jvm

private ConfigurableApplicationContext getContext() {
  return (ConfigurableApplicationContext)getTestContext().getApplicationContext();
}

代码示例来源:origin: spring-projects/spring-framework

public static DataSource retrieveDataSource(TestContext testContext, @Nullable String name) {
  Assert.notNull(testContext, "TestContext must not be null");
  BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();

代码示例来源:origin: spring-projects/spring-framework

private ApplicationContext loadContext(Class<?> testClass) {
  TestContext testContext = TestContextTestUtils.buildTestContext(testClass, contextCache);
  return testContext.getApplicationContext();
}

代码示例来源:origin: stackoverflow.com

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());
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

public static PlatformTransactionManager retrieveTransactionManager(TestContext testContext, @Nullable String name) {
  Assert.notNull(testContext, "TestContext must not be null");
  BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();

代码示例来源:origin: spring-projects/spring-framework

/**
 * 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);
}

代码示例来源:origin: Activiti/Activiti

@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);
 }
}

代码示例来源:origin: spring-projects/spring-framework

ApplicationContext context = testContext.getApplicationContext();

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: spring-projects/spring-framework

@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");
}

代码示例来源:origin: spring-projects/spring-framework

@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");
}

代码示例来源:origin: spring-projects/spring-framework

@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();
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: spring-projects/spring-framework

@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();
}

相关文章