/** * 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; }
private List<Class<?>> classes(TestContextManager testContextManager) { return testContextManager.getTestExecutionListeners().stream().map(Object::getClass).collect(toList()); }
for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.prepareTestInstance(getTestContext());
private void assertNumRegisteredListeners(Class<?> testClass, int expected) { TestContextManager testContextManager = new TestContextManager(testClass); assertEquals("Num registered TELs for " + testClass, expected, testContextManager.getTestExecutionListeners().size()); }
/** * 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); } } }
prepareForBeforeCallback(callbackName, testInstance, testMethod); for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.beforeTestExecution(getTestContext());
prepareForBeforeCallback(callbackName, testInstance, testMethod); for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.beforeTestMethod(getTestContext());
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()); } }
@Test public void listenerExecutionOrder() throws Exception { assertEquals("Registered TestExecutionListeners", 3, this.testContextManager.getTestExecutionListeners().size());
/** * 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; }
/** * 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<TestExecutionListener>(getTestExecutionListeners()); Collections.reverse(listenersReversed); return listenersReversed; }
List<TestExecutionListener> oldListeners = getTestContextManager().getTestExecutionListeners(); List<TestExecutionListener> listeners = new ArrayList<TestExecutionListener>(oldListeners.size()); for (TestExecutionListener old : oldListeners) { standardListeners.addAll(getTestContextManager().getTestExecutionListeners()); for (final TestExecutionListener listener : listeners) { if (!standardListeners.contains(listener)) {
private void ensureDirtyContextListenerIsUsed() { for (TestExecutionListener listener : getTestContextManager().getTestExecutionListeners()) { if (listener instanceof DirtiesContextTestExecutionListener) { return; } } throw new AssertionError( "DirtiesContextTestExecutionListener must be added when using DirtySpringContextJUnit4ClassRunner"); }
for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.prepareTestInstance(getTestContext());
for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.prepareTestInstance(getTestContext());
for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.beforeTestMethod(getTestContext());
for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.beforeTestClass(getTestContext());
/** * 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); } } }
prepareForBeforeCallback(callbackName, testInstance, testMethod); for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.beforeTestExecution(getTestContext());
prepareForBeforeCallback(callbackName, testInstance, testMethod); for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.beforeTestMethod(getTestContext());