Refine search
@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); } }
/** * 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); }
@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); }
/** * 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); }
public TestContextTransactionAttribute(TransactionAttribute targetAttribute, TestContext testContext) { super(targetAttribute); this.name = ClassUtils.getQualifiedMethodName(testContext.getTestMethod(), testContext.getTestClass()); }
private boolean isActivated(TestContext testContext) { return (Boolean.TRUE.equals(testContext.getAttribute(ACTIVATE_LISTENER)) || AnnotatedElementUtils.hasAnnotation(testContext.getTestClass(), WebAppConfiguration.class)); }
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); }
@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(); } }
@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"); }
BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
@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(); } } } }
private void resetMocks(TestContext testContext) { ApplicationContext applicationContext = testContext.getApplicationContext(); Map<String, ResettableSpringockito> beansOfType = applicationContext.getBeansOfType(ResettableSpringockito.class); for (ResettableSpringockito resettableSpringockito : beansOfType.values()) { resettableSpringockito.reset(); } } }
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()); }
/** * 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(); }
/** * 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()); } }
@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(); }
@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 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); }
ApplicationContext context = testContext.getApplicationContext(); testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
@Override public void beforeTestMethod(TestContext testContext) throws Exception { String name = testContext.getTestMethod().getName(); actualMethods.add(name); testContext.setAttribute("method", name); this.methodName.set(name); }