diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/FailingBeforeAndAfterMethodsTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/FailingBeforeAndAfterMethodsTests.java index ec5ffd493f..cf97705037 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/FailingBeforeAndAfterMethodsTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/FailingBeforeAndAfterMethodsTests.java @@ -37,14 +37,24 @@ import org.springframework.test.context.transaction.BeforeTransaction; /** *

- * JUnit 4 based unit test for verifying that 'before' and ' - * after' methods of {@link TestExecutionListener - * TestExecutionListeners} as well as {@link BeforeTransaction - * @BeforeTransaction} and {@link AfterTransaction @AfterTransaction} - * methods can fail a test in a JUnit 4.4 environment, as requested in before' and 'after' + * methods of {@link TestExecutionListener TestExecutionListeners} as well as + * {@link BeforeTransaction @BeforeTransaction} and + * {@link AfterTransaction @AfterTransaction} methods can fail a test in a + * JUnit 4.4 environment, as requested in SPR-3960. *

+ *

+ * Indirectly, this class also verifies that all {@link TestExecutionListener} + * lifecycle callbacks are called. + *

+ *

+ * As of Spring 3.0, this class also tests support for the new + * {@link TestExecutionListener#beforeTestClass(TestContext) beforeTestClass()} + * and {@link TestExecutionListener#afterTestClass(TestContext) + * afterTestClass()} lifecycle callback methods. + *

* * @author Sam Brannen * @since 2.5 @@ -63,10 +73,13 @@ public class FailingBeforeAndAfterMethodsTests { public static Collection testData() { return Arrays.asList(new Object[][] {// // + { AlwaysFailingBeforeTestClassTestCase.class },// + { AlwaysFailingAfterTestClassTestCase.class },// + { AlwaysFailingPrepareTestInstanceTestCase.class },// { AlwaysFailingBeforeTestMethodTestCase.class },// { AlwaysFailingAfterTestMethodTestCase.class },// - { FailingBeforeTransactionalTestCase.class },// - { FailingAfterTransactionalTestCase.class } // + { FailingBeforeTransactionTestCase.class },// + { FailingAfterTransactionTestCase.class } // }); } @@ -77,10 +90,37 @@ public class FailingBeforeAndAfterMethodsTests { notifier.addListener(listener); new SpringJUnit4ClassRunner(this.clazz).run(notifier); - assertEquals("Verifying number of failures for test class [" + this.clazz + "].", 1, listener.getTestFailureCount()); + assertEquals("Verifying number of failures for test class [" + this.clazz + "].", 1, + listener.getTestFailureCount()); } + // ------------------------------------------------------------------- + + static class AlwaysFailingBeforeTestClassTestExecutionListener extends AbstractTestExecutionListener { + + @Override + public void beforeTestClass(TestContext testContext) { + fail("always failing beforeTestClass()"); + } + } + + static class AlwaysFailingAfterTestClassTestExecutionListener extends AbstractTestExecutionListener { + + @Override + public void afterTestClass(TestContext testContext) { + fail("always failing afterTestClass()"); + } + } + + static class AlwaysFailingPrepareTestInstanceTestExecutionListener extends AbstractTestExecutionListener { + + @Override + public void prepareTestInstance(TestContext testContext) throws Exception { + fail("always failing prepareTestInstance()"); + } + } + static class AlwaysFailingBeforeTestMethodTestExecutionListener extends AbstractTestExecutionListener { @Override @@ -97,24 +137,37 @@ public class FailingBeforeAndAfterMethodsTests { } } - @TestExecutionListeners(value = { AlwaysFailingBeforeTestMethodTestExecutionListener.class }, inheritListeners = false) - public static class AlwaysFailingBeforeTestMethodTestCase extends AbstractJUnit4SpringContextTests { + @RunWith(SpringJUnit4ClassRunner.class) + @TestExecutionListeners( {}) + public static class BaseTestCase { @Test public void testNothing() { } } - @TestExecutionListeners(value = { AlwaysFailingAfterTestMethodTestExecutionListener.class }, inheritListeners = false) - public static class AlwaysFailingAfterTestMethodTestCase extends AbstractJUnit4SpringContextTests { - - @Test - public void testNothing() { - } + @TestExecutionListeners(AlwaysFailingBeforeTestClassTestExecutionListener.class) + public static class AlwaysFailingBeforeTestClassTestCase extends BaseTestCase { } - @ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" }) - public static class FailingBeforeTransactionalTestCase extends AbstractTransactionalJUnit4SpringContextTests { + @TestExecutionListeners(AlwaysFailingAfterTestClassTestExecutionListener.class) + public static class AlwaysFailingAfterTestClassTestCase extends BaseTestCase { + } + + @TestExecutionListeners(AlwaysFailingPrepareTestInstanceTestExecutionListener.class) + public static class AlwaysFailingPrepareTestInstanceTestCase extends BaseTestCase { + } + + @TestExecutionListeners(AlwaysFailingBeforeTestMethodTestExecutionListener.class) + public static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase { + } + + @TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class) + public static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase { + } + + @ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml") + public static class FailingBeforeTransactionTestCase extends AbstractTransactionalJUnit4SpringContextTests { @Test public void testNothing() { @@ -126,8 +179,8 @@ public class FailingBeforeAndAfterMethodsTests { } } - @ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" }) - public static class FailingAfterTransactionalTestCase extends AbstractTransactionalJUnit4SpringContextTests { + @ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml") + public static class FailingAfterTransactionTestCase extends AbstractTransactionalJUnit4SpringContextTests { @Test public void testNothing() {