本文整理了Java中org.springframework.test.context.TestContext
类的一些代码示例,展示了TestContext
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TestContext
类的具体详情如下:
包路径:org.springframework.test.context.TestContext
类名称:TestContext
[英]TestContext encapsulates the context in which a test is executed, agnostic of the actual testing framework in use.
[中]TestContext封装了执行测试的上下文,与实际使用的测试框架无关。
代码示例来源: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
/**
* 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
@Test
public void beforeAndAfterTestMethodForDirtiesContextDeclaredLocallyOnMethodWithBeforeMethodMode() throws Exception {
Class<?> clazz = getClass();
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(
clazz.getDeclaredMethod("dirtiesContextDeclaredLocallyWithBeforeMethodMode"));
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Mark the {@linkplain ApplicationContext application context} of the supplied
* {@linkplain TestContext test context} as
* {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty}
* and set {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
* REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}.
* @param testContext the test context whose application context should
* be marked as dirty
* @param hierarchyMode the context cache clearing mode to be applied if the
* context is part of a hierarchy; may be {@code null}
* @since 3.2.2
*/
protected void dirtyContext(TestContext testContext, @Nullable HierarchyMode hierarchyMode) {
testContext.markApplicationContextDirty(hierarchyMode);
testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}
代码示例来源:origin: spring-projects/spring-framework
public TestContextTransactionAttribute(TransactionAttribute targetAttribute, TestContext testContext) {
super(targetAttribute);
this.name = ClassUtils.getQualifiedMethodName(testContext.getTestMethod(), testContext.getTestClass());
}
代码示例来源:origin: spring-projects/spring-framework
private boolean isActivated(TestContext testContext) {
return (Boolean.TRUE.equals(testContext.getAttribute(ACTIVATE_LISTENER)) ||
AnnotatedElementUtils.hasAnnotation(testContext.getTestClass(), WebAppConfiguration.class));
}
代码示例来源:origin: ru.yandex.qatools.camelot/camelot-test
private MockedClientSenderInitializer getClientSenderInitializer(TestContext testContext, boolean throwOnEmpty) {
if (testContext.getAttribute(CLIENT_INITIALIZER) == null) {
final ApplicationContext appContext = getAppContext(testContext, throwOnEmpty);
if (appContext != null) {
testContext.setAttribute(CLIENT_INITIALIZER, appContext.getBean(MockedClientSenderInitializer.class));
}
}
return (MockedClientSenderInitializer) testContext.getAttribute(CLIENT_INITIALIZER);
}
代码示例来源:origin: org.springframework.boot/spring-boot-test-autoconfigure
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
ApplicationContext applicationContext = testContext.getApplicationContext();
String[] names = applicationContext
.getBeanNamesForType(MockRestServiceServer.class, false, false);
for (String name : names) {
applicationContext.getBean(name, MockRestServiceServer.class).reset();
}
}
代码示例来源: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
BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
代码示例来源:origin: com.github.fridujo/spring-automocker
@Override
public void afterTestMethod(TestContext testContext) {
ResetMocks resetMocks = AnnotationUtils.getAnnotation(testContext.getTestClass(), ResetMocks.class);
if (resetMocks != null && !resetMocks.disable()) {
ApplicationContext applicationContext = testContext.getApplicationContext();
for (Resettable resettable : applicationContext.getBeansOfType(Resettable.class).values()) {
resettable.reset();
}
}
}
}
代码示例来源:origin: org.kubek2k/springockito-annotations
private void resetMocks(TestContext testContext) {
ApplicationContext applicationContext = testContext.getApplicationContext();
Map<String, ResettableSpringockito> beansOfType = applicationContext.getBeansOfType(ResettableSpringockito.class);
for (ResettableSpringockito resettableSpringockito : beansOfType.values()) {
resettableSpringockito.reset();
}
}
}
代码示例来源:origin: spring-projects/spring-framework
private void assertBeforeTestMethodWithNonTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception {
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
Invocable instance = BeanUtils.instantiateClass(clazz);
given(testContext.getTestInstance()).willReturn(instance);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest"));
assertFalse("callback should not have been invoked", instance.invoked());
TransactionContextHolder.removeCurrentTransactionContext();
listener.beforeTestMethod(testContext);
assertFalse("callback should not have been invoked", instance.invoked());
}
代码示例来源: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
/**
* Run all {@link BeforeTransaction @BeforeTransaction} methods for the
* specified {@linkplain TestContext test context}. If one of the methods
* fails, however, the caught exception will be rethrown in a wrapped
* {@link RuntimeException}, and the remaining methods will <strong>not</strong>
* be given a chance to execute.
* @param testContext the current test context
*/
protected void runBeforeTransactionMethods(TestContext testContext) throws Exception {
try {
List<Method> methods = getAnnotatedMethods(testContext.getTestClass(), BeforeTransaction.class);
Collections.reverse(methods);
for (Method method : methods) {
if (logger.isDebugEnabled()) {
logger.debug("Executing @BeforeTransaction method [" + method + "] for test context " + testContext);
}
ReflectionUtils.makeAccessible(method);
method.invoke(testContext.getTestInstance());
}
}
catch (InvocationTargetException ex) {
if (logger.isErrorEnabled()) {
logger.error("Exception encountered while executing @BeforeTransaction methods for test context " +
testContext + ".", ex.getTargetException());
}
ReflectionUtils.rethrowException(ex.getTargetException());
}
}
代码示例来源: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: 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 beforeAndAfterTestClassForDirtiesContextDeclaredLocallyOnClassBeforeClass() throws Exception {
Class<?> clazz = DirtiesContextDeclaredLocallyBeforeClass.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
beforeListener.beforeTestClass(testContext);
afterListener.beforeTestClass(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
afterListener.afterTestClass(testContext);
beforeListener.afterTestClass(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
代码示例来源:origin: spring-projects/spring-framework
ApplicationContext context = testContext.getApplicationContext();
testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
代码示例来源:origin: spring-projects/spring-framework
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
String name = testContext.getTestMethod().getName();
actualMethods.add(name);
testContext.setAttribute("method", name);
this.methodName.set(name);
}
内容来源于网络,如有侵权,请联系作者删除!