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

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

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

TestContext.removeAttribute介绍

暂无

代码示例

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

/**
 * If the {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} in the supplied
 * {@code TestContext} has a value of {@link Boolean#TRUE}, this method will
 * (1) clean up thread-local state after each test method by {@linkplain
 * RequestContextHolder#resetRequestAttributes() resetting} Spring Web's
 * {@code RequestContextHolder} and (2) ensure that new mocks are injected
 * into the test instance for subsequent tests by setting the
 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
 * in the test context to {@code true}.
 * <p>The {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} and
 * {@link #POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} will be subsequently
 * removed from the test context, regardless of their values.
 * @see TestExecutionListener#afterTestMethod(TestContext)
 */
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
  if (Boolean.TRUE.equals(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE))) {
    if (logger.isDebugEnabled()) {
      logger.debug(String.format("Resetting RequestContextHolder for test context %s.", testContext));
    }
    RequestContextHolder.resetRequestAttributes();
    testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE,
      Boolean.TRUE);
  }
  testContext.removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
  testContext.removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
}

代码示例来源: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: spring-projects/spring-framework

private void assertWebAppConfigTestCase() throws Exception {
  listener.prepareTestInstance(testContext);
  assertRequestAttributesExist();
  assertSetUpOutsideOfStelAttributeDoesNotExist();
  verify(testContext, times(1)).setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
  verify(testContext, times(1)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
  given(testContext.getAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(Boolean.TRUE);
  given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(Boolean.TRUE);
  listener.beforeTestMethod(testContext);
  assertRequestAttributesExist();
  assertSetUpOutsideOfStelAttributeDoesNotExist();
  verify(testContext, times(1)).setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
  verify(testContext, times(1)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
  listener.afterTestMethod(testContext);
  verify(testContext).removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
  verify(testContext).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
  assertRequestAttributesDoNotExist();
}

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

@Test
public void legacyWebTestCaseWithoutExistingRequestAttributes() throws Exception {
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(LegacyWebTestCase.class);
  RequestContextHolder.resetRequestAttributes();
  assertRequestAttributesDoNotExist();
  listener.beforeTestClass(testContext);
  listener.prepareTestInstance(testContext);
  assertRequestAttributesDoNotExist();
  verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
  given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null);
  listener.beforeTestMethod(testContext);
  assertRequestAttributesDoNotExist();
  verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
  listener.afterTestMethod(testContext);
  verify(testContext, times(1)).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
  assertRequestAttributesDoNotExist();
}

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

@Test
public void legacyWebTestCaseWithPresetRequestAttributes() throws Exception {
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(LegacyWebTestCase.class);
  listener.beforeTestClass(testContext);
  assertSetUpOutsideOfStelAttributeExists();
  listener.prepareTestInstance(testContext);
  assertSetUpOutsideOfStelAttributeExists();
  verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
  given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null);
  listener.beforeTestMethod(testContext);
  assertSetUpOutsideOfStelAttributeExists();
  verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
  given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null);
  listener.afterTestMethod(testContext);
  verify(testContext, times(1)).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
  assertSetUpOutsideOfStelAttributeExists();
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * If configured before test execution sets the SecurityContext
 * @since 5.1
 */
@Override
public void beforeTestExecution(TestContext testContext) {
  SecurityContext securityContext = (SecurityContext) testContext.removeAttribute(SECURITY_CONTEXT_ATTR_NAME);
  if (securityContext != null) {
    TestSecurityContextHolder.setContext(securityContext);
  }
}

代码示例来源:origin: org.springframework.security/spring-security-test

/**
 * If configured before test execution sets the SecurityContext
 * @since 5.1
 */
@Override
public void beforeTestExecution(TestContext testContext) {
  SecurityContext securityContext = (SecurityContext) testContext.removeAttribute(SECURITY_CONTEXT_ATTR_NAME);
  if (securityContext != null) {
    TestSecurityContextHolder.setContext(securityContext);
  }
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-test

/**
 * If the {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} in the supplied
 * {@code TestContext} has a value of {@link Boolean#TRUE}, this method will
 * (1) clean up thread-local state after each test method by {@linkplain
 * RequestContextHolder#resetRequestAttributes() resetting} Spring Web's
 * {@code RequestContextHolder} and (2) ensure that new mocks are injected
 * into the test instance for subsequent tests by setting the
 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
 * in the test context to {@code true}.
 * <p>The {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} and
 * {@link #POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} will be subsequently
 * removed from the test context, regardless of their values.
 * @see TestExecutionListener#afterTestMethod(TestContext)
 */
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
  if (Boolean.TRUE.equals(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE))) {
    if (logger.isDebugEnabled()) {
      logger.debug(String.format("Resetting RequestContextHolder for test context %s.", testContext));
    }
    RequestContextHolder.resetRequestAttributes();
    testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE,
      Boolean.TRUE);
  }
  testContext.removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
  testContext.removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * If the {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} in the supplied
 * {@code TestContext} has a value of {@link Boolean#TRUE}, this method will
 * (1) clean up thread-local state after each test method by {@linkplain
 * RequestContextHolder#resetRequestAttributes() resetting} Spring Web's
 * {@code RequestContextHolder} and (2) ensure that new mocks are injected
 * into the test instance for subsequent tests by setting the
 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
 * in the test context to {@code true}.
 * <p>The {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} and
 * {@link #POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} will be subsequently
 * removed from the test context, regardless of their values.
 * @see TestExecutionListener#afterTestMethod(TestContext)
 */
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
  if (Boolean.TRUE.equals(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE))) {
    if (logger.isDebugEnabled()) {
      logger.debug(String.format("Resetting RequestContextHolder for test context %s.", testContext));
    }
    RequestContextHolder.resetRequestAttributes();
    testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE,
      Boolean.TRUE);
  }
  testContext.removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
  testContext.removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
}

代码示例来源:origin: peholmst/vaadin4spring

private synchronized void tearDownVaadinScopesIfNecessary(TestContext testContext) {
    if (notAnnotatedWithVaadinAppConfiguration(testContext) || !alreadySetUpVaadinScopes(testContext)) {
      logger.debug("No need to tear down Vaadin scopes for test context [{}]", testContext);
      return;
    }
    VaadinScopes.tearDown();
    testContext.removeAttribute(SET_UP_SCOPES_ATTRIBUTE);
  }
}

代码示例来源:origin: org.vaadin.spring/spring-vaadin-test

private synchronized void tearDownVaadinScopesIfNecessary(TestContext testContext) {
    if (notAnnotatedWithVaadinAppConfiguration(testContext) || !alreadySetUpVaadinScopes(testContext)) {
      logger.debug("No need to tear down Vaadin scopes for test context [{}]", testContext);
      return;
    }
    VaadinScopes.tearDown();
    testContext.removeAttribute(SET_UP_SCOPES_ATTRIBUTE);
  }
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * 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();
  AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
  beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
  beanFactory.initializeBean(bean, testContext.getTestClass().getName());
  testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-test

/**
 * 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: stackoverflow.com

protected void injectDependencies(final TestContext testContext) throws Exception {
   Object bean = testContext.getTestInstance();
   AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
   beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
   beanFactory.initializeBean(bean, testContext.getTestClass().getName());
   testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
 }

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

protected void injectDependencies(final TestContext testContext) throws Exception {
  Object bean = testContext.getTestInstance();
  AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
  beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
  beanFactory.initializeBean(bean, testContext.getTestClass().getName());
  testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}

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

public class DITestExecutionListener extends DependencyInjectionTestExecutionListener {
 protected void injectDependencies(final TestContext testContext) throws Exception {
   INITSTUFF();
   Object bean = testContext.getTestInstance();
   AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
   beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
   beanFactory.initializeBean(bean, testContext.getTestClass().getName());
   testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
 }

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

protected void injectDependencies(final TestContext testContext)
throws Exception {
  Object bean = testContext.getTestInstance();
  AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext()
      .getAutowireCapableBeanFactory();
  beanFactory.autowireBeanProperties(bean, 

      AutowireCapableBeanFactory.AUTOWIRE_NO,
      // no autowiring!!!!!!!!

      false
    );

  beanFactory.initializeBean(bean, testContext.getTestClass().getName());
  // but here, bean post processors are run

  testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}

相关文章