/** * 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()); } }
method.invoke(testContext.getTestInstance());
/** * 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); }
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()); }
private void assertAfterTestMethodWithNonTransactionalTestMethod(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); listener.afterTestMethod(testContext); assertFalse("callback should not have been invoked", instance.invoked()); }
/** * Discover a {@link JobExecution} as a field in the test case or create * one if none is available. * * @param testContext the current test context * @return a {@link JobExecution} */ protected JobExecution getJobExecution(TestContext testContext) { Object target = testContext.getTestInstance(); ExtractorMethodCallback method = new ExtractorMethodCallback(JobExecution.class, "getJobExecution"); ReflectionUtils.doWithMethods(target.getClass(), method); if (method.getName() != null) { HippyMethodInvoker invoker = new HippyMethodInvoker(); invoker.setTargetObject(target); invoker.setTargetMethod(method.getName()); try { invoker.prepare(); return (JobExecution) invoker.invoke(); } catch (Exception e) { throw new IllegalArgumentException("Could not create job execution from method: " + method.getName(), e); } } return MetaDataInstanceFactory.createJobExecution(); }
private void assertBeforeTestMethodWithTransactionalTestMethod(Class<? extends Invocable> clazz, boolean invokedInTx) 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("transactionalTest")); assertFalse("callback should not have been invoked", instance.invoked()); TransactionContextHolder.removeCurrentTransactionContext(); listener.beforeTestMethod(testContext); assertEquals(invokedInTx, instance.invoked()); }
@Test // SPR-13895 public void transactionalTestWithoutTransactionManager() throws Exception { TransactionalTestExecutionListener listener = new TransactionalTestExecutionListener() { protected PlatformTransactionManager getTransactionManager(TestContext testContext, String qualifier) { return null; } }; Class<? extends Invocable> clazz = TransactionalDeclaredOnClassLocallyTestCase.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); Invocable instance = BeanUtils.instantiateClass(clazz); given(testContext.getTestInstance()).willReturn(instance); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest")); assertFalse("callback should not have been invoked", instance.invoked()); TransactionContextHolder.removeCurrentTransactionContext(); try { listener.beforeTestMethod(testContext); fail("Should have thrown an IllegalStateException"); } catch (IllegalStateException e) { assertTrue(e.getMessage().startsWith( "Failed to retrieve PlatformTransactionManager for @Transactional test")); } }
private void assertAfterTestMethodWithTransactionalTestMethod(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("transactionalTest")); given(tm.getTransaction(BDDMockito.any(TransactionDefinition.class))).willReturn(new SimpleTransactionStatus()); assertFalse("callback should not have been invoked", instance.invoked()); TransactionContextHolder.removeCurrentTransactionContext(); listener.beforeTestMethod(testContext); assertFalse("callback should not have been invoked", instance.invoked()); listener.afterTestMethod(testContext); assertTrue("callback should have been invoked", instance.invoked()); }
/** * Convenience method to call the given method on the current test class as denoted by the * TestContext * * @param testContext current context that has the current test class/method on it * @param methodName the name of the method to invoke * @throws Exception */ private void callMethod(TestContext testContext, String methodName) throws Exception { Method method; try { method = testContext.getTestClass().getMethod(methodName); } catch (NoSuchMethodException e) { throw new Exception("There is no method named '" + methodName + "' on the " + testContext.getTestClass() + " class", e); } method.invoke(testContext.getTestInstance()); }
@Override public void prepareTestInstance(final TestContext testContext) { MockitoAnnotations.initMocks(testContext.getTestInstance()); } }
@Override public void beforeTestMethod(TestContext testContext) throws Exception { // FIXME: Is there a better way to inject the instance into the test class? if (testContext.getTestInstance() instanceof TestContextAware) { System.err.println("injecting TestContext into TestContextAware test: " + testContext.getTestInstance().getClass().getSimpleName() + "." + testContext.getTestMethod().getName()); ((TestContextAware) testContext.getTestInstance()).setTestContext(testContext); } } }
@Override public void beforeTestMethod(final TestContext testContext) throws Exception { //System.err.println(String.format("TemporaryDatabaseExecutionListener.beforeTestMethod(%s)", testContext)); // FIXME: Is there a better way to inject the instance into the test class? if (testContext.getTestInstance() instanceof TemporaryDatabaseAware<?>) { //System.err.println("injecting TemporaryDatabase into TemporaryDatabaseAware test: " // + testContext.getTestInstance().getClass().getSimpleName() + "." // + testContext.getTestMethod().getName()); injectTemporaryDatabase(testContext); } }
private void initMocks(TestContext testContext) { if (hasMockitoAnnotations(testContext)) { MockitoAnnotations.initMocks(testContext.getTestInstance()); } }
@Override public void beforeTestMethod(final TestContext testContext) throws Exception { //System.err.println(String.format("TemporaryDatabaseExecutionListener.beforeTestMethod(%s)", testContext)); // FIXME: Is there a better way to inject the instance into the test class? if (testContext.getTestInstance() instanceof TemporaryDatabaseAware<?>) { //System.err.println("injecting TemporaryDatabase into TemporaryDatabaseAware test: " // + testContext.getTestInstance().getClass().getSimpleName() + "." // + testContext.getTestMethod().getName()); injectTemporaryDatabase(testContext); } }
@Override public void afterTestMethod(TestContext testContext) { results = ((ScenarioHandler)testContext.getTestInstance()).getResults(); scenario.awaitUntilComplete(); }
private void reinjectFields(final TestContext testContext) { postProcessFields(testContext, (mockitoField, postProcessor) -> { ReflectionUtils.makeAccessible(mockitoField.field); ReflectionUtils.setField(mockitoField.field, testContext.getTestInstance(), null); postProcessor.inject(mockitoField.field, mockitoField.target, mockitoField.definition); }); }
/** * As the name suggests, this runs before each test method. Ipsedixit will inspect and populate your test object * at this point. * * @param testContext the {@link org.springframework.test.context.TestContext}, as passed through from Spring. * @throws Exception is something goes bump in the night (or day, for that matter). */ public void beforeTestMethod(TestContext testContext) throws Exception { Class<?> testClass = testContext.getTestClass(); Configuration configuration = getConfiguration(testClass); DataPopulator dataPopulator = getDefaultDataPopulator(configuration); dataPopulator.populate(testContext.getTestInstance()); }
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); }
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); }