@Test public void invokeTestContextManagerFromConcurrentThreads() { TestContextManager tcm = new TestContextManager(TestCase.class); // Run the actual test several times in order to increase the chance of threads // stepping on each others' toes by overwriting the same mutable state in the // TestContext. IntStream.range(1, 20).forEach(i -> { actualMethods.clear(); // Execute TestExecutionListener in parallel, thereby simulating parallel // test method execution. stream(TestCase.class.getDeclaredMethods()).parallel().forEach(testMethod -> { try { tcm.beforeTestClass(); tcm.beforeTestMethod(testInstance, testMethod); // no need to invoke the actual test method tcm.afterTestMethod(testInstance, testMethod, null); tcm.afterTestClass(); } catch (Exception ex) { throw new RuntimeException(ex); } }); assertThat(actualMethods, equalTo(expectedMethods)); }); assertEquals(0, tcm.getTestContext().attributeNames().length); }
/** * Delegates to the configured {@link TestContextManager} to * {@linkplain TestContextManager#prepareTestInstance(Object) prepare} this test * instance prior to execution of any individual tests, for example for * injecting dependencies, etc. * @throws Exception if a registered TestExecutionListener throws an exception */ @BeforeClass(alwaysRun = true, dependsOnMethods = "springTestContextBeforeTestClass") protected void springTestContextPrepareTestInstance() throws Exception { this.testContextManager.prepareTestInstance(this); }
public SpringTestContextManager(Class<?> testClass) { delegate = new TestContextManager(testClass); if (!testContextBootstrapperAvailable) { delegate.registerTestExecutionListeners(new SpringMockTestExecutionListener()); } }
prepareForBeforeCallback(callbackName, testInstance, testMethod); for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.beforeTestMethod(getTestContext()); handleBeforeException(ex, callbackName, testExecutionListener, testInstance, testMethod);
/** * Hook for pre-processing a test class <em>before</em> execution of any * tests within the class. Should be called prior to any framework-specific * <em>before class methods</em> (e.g., methods annotated with JUnit 4's * {@link org.junit.BeforeClass @BeforeClass}). * <p>An attempt will be made to give each registered * {@link TestExecutionListener} a chance to pre-process the test class * execution. If a listener throws an exception, however, the remaining * registered listeners will <strong>not</strong> be called. * @throws Exception if a registered TestExecutionListener throws an * exception * @since 3.0 * @see #getTestExecutionListeners() */ public void beforeTestClass() throws Exception { Class<?> testClass = getTestContext().getTestClass(); if (logger.isTraceEnabled()) { logger.trace("beforeTestClass(): class [" + testClass.getName() + "]"); } getTestContext().updateState(null, null, null); for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.beforeTestClass(getTestContext()); } catch (Throwable ex) { logException(ex, "beforeTestClass", testExecutionListener, testClass); ReflectionUtils.rethrowException(ex); } } }
/** * Create a new {@link TestContextManager} for the supplied test class. * <p>Can be overridden by subclasses. * @param clazz the test class to be managed */ protected TestContextManager createTestContextManager(Class<?> clazz) { return new TestContextManager(clazz); }
@Test public void listenerExecutionOrder() throws Exception { assertEquals("Registered TestExecutionListeners", 3, this.testContextManager.getTestExecutionListeners().size()); this.testContextManager.beforeTestMethod(this, this.testMethod); assertExecutionOrder("beforeTestMethod", "beforeTestMethod-1", ); this.testContextManager.beforeTestExecution(this, this.testMethod); assertExecutionOrder("beforeTestExecution", "beforeTestMethod-1", ); this.testContextManager.afterTestExecution(this, this.testMethod, null); assertExecutionOrder("afterTestExecution", "beforeTestMethod-1", ); this.testContextManager.afterTestMethod(this, this.testMethod, null); assertExecutionOrder("afterTestMethod", "beforeTestMethod-1",
testContextManager = new TestContextManager(getClass()); testContextManager.prepareTestInstance(this);
private void assertNumRegisteredListeners(Class<?> testClass, int expected) { TestContextManager testContextManager = new TestContextManager(testClass); assertEquals("Num registered TELs for " + testClass, expected, testContextManager.getTestExecutionListeners().size()); }
/** * Delegates to the configured {@link TestContextManager} to * {@linkplain TestContextManager#afterTestMethod(Object, Method, Throwable) * post-process} the test method after the actual test has executed. * * @param testMethod the test method which has just been executed on the * test instance * @throws Exception allows all exceptions to propagate */ @AfterMethod(alwaysRun = true) protected void springTestContextAfterTestMethod(Method testMethod) throws Exception { try { this.testContextManager.afterTestMethod(this, testMethod, this.testException); } finally { this.testException = null; } }
/** * Delegates to the configured {@link TestContextManager} to * {@linkplain TestContextManager#beforeTestMethod(Object,Method) pre-process} * the test method before the actual test is executed. * @param testMethod the test method which is about to be executed * @throws Exception allows all exceptions to propagate */ @BeforeMethod(alwaysRun = true) protected void springTestContextBeforeTestMethod(Method testMethod) throws Exception { this.testContextManager.beforeTestMethod(this, testMethod); }
@Override public void runBare() throws Throwable { testContextManager.prepareTestInstance(this); // this will initialize all dependencies try { super.runBare(); } finally { testContextManager.afterTestClass(); applicationContext.close(); clearTestContextCache(testContextManager); applicationContext = null; processEngine = null; testContextManager = null; CachedIntrospectionResults.clearClassLoader(getClass().getClassLoader()); } }
/** * Register the supplied list of {@link TestExecutionListener TestExecutionListeners} * by appending them to the list of listeners used by this {@code TestContextManager}. * @see #registerTestExecutionListeners(TestExecutionListener...) */ public void registerTestExecutionListeners(List<TestExecutionListener> testExecutionListeners) { registerTestExecutionListeners(testExecutionListeners.toArray(new TestExecutionListener[0])); }
/** * Delegates to the configured {@link TestContextManager} to call * {@linkplain TestContextManager#afterTestClass() 'after test class'} callbacks. * @throws Exception if a registered TestExecutionListener throws an exception */ @AfterClass(alwaysRun = true) protected void springTestContextAfterTestClass() throws Exception { this.testContextManager.afterTestClass(); }
logger.trace("prepareTestInstance(): instance [" + testInstance + "]"); getTestContext().updateState(testInstance, null, null); for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.prepareTestInstance(getTestContext());
/** * Delegates to the configured {@link TestContextManager} to call * {@linkplain TestContextManager#beforeTestClass() 'before test class'} callbacks. * @throws Exception if a registered TestExecutionListener throws an exception */ @BeforeClass(alwaysRun = true) protected void springTestContextBeforeTestClass() throws Exception { this.testContextManager.beforeTestClass(); }
List<TestExecutionListener> oldListeners = getTestContextManager().getTestExecutionListeners(); List<TestExecutionListener> listeners = new ArrayList<TestExecutionListener>(oldListeners.size()); for (TestExecutionListener old : oldListeners) { try { final TestExecutionListener listener = (TestExecutionListener)Class.forName(className).newInstance(); getTestContextManager().registerTestExecutionListeners(listener); } catch (final Exception e) { LOG.info("Failed while attempting to load default unit test listener class {}: {}", className, e.getLocalizedMessage()); standardListeners.addAll(getTestContextManager().getTestExecutionListeners()); for (final TestExecutionListener listener : listeners) { if (!standardListeners.contains(listener)) { getTestContextManager().registerTestExecutionListeners(listener);
/** * Get a copy of the {@link TestExecutionListener TestExecutionListeners} * registered for this {@code TestContextManager} in reverse order. */ private List<TestExecutionListener> getReversedTestExecutionListeners() { List<TestExecutionListener> listenersReversed = new ArrayList<>(getTestExecutionListeners()); Collections.reverse(listenersReversed); return listenersReversed; }
/** * Construct a new AbstractTestNGSpringContextTests instance and initialize * the internal {@link TestContextManager} for the current test class. */ public AbstractTestNGSpringContextTests() { this.testContextManager = new TestContextManager(getClass()); }
private void test(String useCase, Class<?> testClass, Callback callback) throws Exception { TestContextManager testContextManager = new TestContextManager(testClass); assertEquals("Registered TestExecutionListeners", 2, testContextManager.getTestExecutionListeners().size()); try { Method testMethod = getClass().getMethod("toString"); callback.invoke(testContextManager, testClass, testMethod); fail("should have thrown an AssertionError"); } catch (AssertionError err) { // 'after' callbacks are reversed, so 2 comes before 1. assertEquals(useCase + "-2", err.getMessage()); Throwable[] suppressed = err.getSuppressed(); assertEquals(1, suppressed.length); assertEquals(useCase + "-1", suppressed[0].getMessage()); } }