Skip to content

Commit 1f087b4

Browse files
committed
[SPR-4702] Explicit tests for TestExecutionListener's new beforeTestClass() and afterTestClass()} lifecycle callback methods with TestNG support classes.
1 parent 0483cb5 commit 1f087b4

File tree

2 files changed

+80
-37
lines changed

2 files changed

+80
-37
lines changed

org.springframework.test/src/test/java/org/springframework/test/context/junit4/FailingBeforeAndAfterMethodsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void afterTestMethod(TestContext testContext) {
139139

140140
@RunWith(SpringJUnit4ClassRunner.class)
141141
@TestExecutionListeners( {})
142-
public static class BaseTestCase {
142+
public static abstract class BaseTestCase {
143143

144144
@Test
145145
public void testNothing() {

org.springframework.test/src/test/java/org/springframework/test/context/testng/FailingBeforeAndAfterMethodsTests.java

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,30 +25,39 @@
2525
import org.junit.runner.RunWith;
2626
import org.junit.runners.Parameterized;
2727
import org.junit.runners.Parameterized.Parameters;
28-
import org.testng.ITestContext;
29-
import org.testng.ITestListener;
30-
import org.testng.ITestResult;
31-
import org.testng.TestNG;
32-
3328
import org.springframework.test.context.ContextConfiguration;
3429
import org.springframework.test.context.TestContext;
3530
import org.springframework.test.context.TestExecutionListener;
3631
import org.springframework.test.context.TestExecutionListeners;
3732
import org.springframework.test.context.support.AbstractTestExecutionListener;
3833
import org.springframework.test.context.transaction.AfterTransaction;
3934
import org.springframework.test.context.transaction.BeforeTransaction;
35+
import org.testng.ITestContext;
36+
import org.testng.ITestListener;
37+
import org.testng.ITestResult;
38+
import org.testng.TestNG;
4039

4140
/**
4241
* <p>
43-
* JUnit 4 based unit test for verifying that '<em>before</em>' and '<em>after</em>'
42+
* JUnit 4 based unit test for verifying that '<i>before</i>' and '<i>after</i>'
4443
* methods of {@link TestExecutionListener TestExecutionListeners} as well as
45-
* {@link BeforeTransaction @BeforeTransaction} and
46-
* {@link AfterTransaction @AfterTransaction} methods can fail a test in a
44+
* {@link BeforeTransaction &#064;BeforeTransaction} and
45+
* {@link AfterTransaction &#064;AfterTransaction} methods can fail a test in a
4746
* TestNG environment, as requested in <a
4847
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3960"
4948
* target="_blank">SPR-3960</a>.
5049
* </p>
51-
*
50+
* <p>
51+
* Indirectly, this class also verifies that all {@link TestExecutionListener}
52+
* lifecycle callbacks are called.
53+
* </p>
54+
* <p>
55+
* As of Spring 3.0, this class also tests support for the new
56+
* {@link TestExecutionListener#beforeTestClass(TestContext) beforeTestClass()}
57+
* and {@link TestExecutionListener#afterTestClass(TestContext)
58+
* afterTestClass()} lifecycle callback methods.
59+
* </p>
60+
*
5261
* @author Sam Brannen
5362
* @since 2.5
5463
*/
@@ -74,16 +83,15 @@ public FailingBeforeAndAfterMethodsTests(final Class<?> clazz, final int expecte
7483

7584
@Parameters
7685
public static Collection<Object[]> testData() {
77-
return Arrays.asList(new Object[][] {
78-
79-
{ AlwaysFailingBeforeTestMethodTestCase.class, 1, 0, 0, 1 },
80-
81-
{ AlwaysFailingAfterTestMethodTestCase.class, 1, 1, 0, 1 },
82-
83-
{ FailingBeforeTransactionalTestCase.class, 1, 0, 0, 1 },
84-
85-
{ FailingAfterTransactionalTestCase.class, 1, 1, 0, 1 }
86-
86+
return Arrays.asList(new Object[][] {//
87+
//
88+
{ AlwaysFailingBeforeTestClassTestCase.class, 1, 0, 0, 1 },//
89+
{ AlwaysFailingAfterTestClassTestCase.class, 1, 1, 0, 1 },//
90+
{ AlwaysFailingPrepareTestInstanceTestCase.class, 1, 0, 0, 1 },//
91+
{ AlwaysFailingBeforeTestMethodTestCase.class, 1, 0, 0, 1 },//
92+
{ AlwaysFailingAfterTestMethodTestCase.class, 1, 1, 0, 1 },//
93+
{ FailingBeforeTransactionTestCase.class, 1, 0, 0, 1 },//
94+
{ FailingAfterTransactionTestCase.class, 1, 1, 0, 1 } //
8795
});
8896
}
8997

@@ -97,13 +105,13 @@ public void runTestAndAssertCounters() throws Exception {
97105
testNG.run();
98106

99107
assertEquals("Verifying number of test starts for test class [" + this.clazz + "].",
100-
this.expectedTestStartCount, listener.testStartCount);
108+
this.expectedTestStartCount, listener.testStartCount);
101109
assertEquals("Verifying number of successful tests for test class [" + this.clazz + "].",
102-
this.expectedTestSuccessCount, listener.testSuccessCount);
110+
this.expectedTestSuccessCount, listener.testSuccessCount);
103111
assertEquals("Verifying number of failures for test class [" + this.clazz + "].", this.expectedFailureCount,
104-
listener.testFailureCount);
112+
listener.testFailureCount);
105113
assertEquals("Verifying number of failed configurations for test class [" + this.clazz + "].",
106-
this.expectedFailedConfigurationsCount, listener.failedConfigurationsCount);
114+
this.expectedFailedConfigurationsCount, listener.failedConfigurationsCount);
107115
}
108116

109117

@@ -141,6 +149,31 @@ public void onTestSuccess(ITestResult testResult) {
141149
}
142150
}
143151

152+
// -------------------------------------------------------------------
153+
154+
static class AlwaysFailingBeforeTestClassTestExecutionListener extends AbstractTestExecutionListener {
155+
156+
@Override
157+
public void beforeTestClass(TestContext testContext) {
158+
org.testng.Assert.fail("always failing beforeTestClass()");
159+
}
160+
}
161+
162+
static class AlwaysFailingAfterTestClassTestExecutionListener extends AbstractTestExecutionListener {
163+
164+
@Override
165+
public void afterTestClass(TestContext testContext) {
166+
org.testng.Assert.fail("always failing afterTestClass()");
167+
}
168+
}
169+
170+
static class AlwaysFailingPrepareTestInstanceTestExecutionListener extends AbstractTestExecutionListener {
171+
172+
@Override
173+
public void prepareTestInstance(TestContext testContext) throws Exception {
174+
org.testng.Assert.fail("always failing prepareTestInstance()");
175+
}
176+
}
144177

145178
static class AlwaysFailingBeforeTestMethodTestExecutionListener extends AbstractTestExecutionListener {
146179

@@ -158,27 +191,38 @@ public void afterTestMethod(TestContext testContext) {
158191
}
159192
}
160193

194+
// -------------------------------------------------------------------
161195

162-
@TestExecutionListeners(value = { AlwaysFailingBeforeTestMethodTestExecutionListener.class }, inheritListeners = false)
163-
public static class AlwaysFailingBeforeTestMethodTestCase extends AbstractTestNGSpringContextTests {
196+
@TestExecutionListeners(value = {}, inheritListeners = false)
197+
public static abstract class BaseTestCase extends AbstractTestNGSpringContextTests {
164198

165199
@org.testng.annotations.Test
166200
public void testNothing() {
167201
}
168202
}
169203

204+
@TestExecutionListeners(AlwaysFailingBeforeTestClassTestExecutionListener.class)
205+
public static class AlwaysFailingBeforeTestClassTestCase extends BaseTestCase {
206+
}
170207

171-
@TestExecutionListeners(value = { AlwaysFailingAfterTestMethodTestExecutionListener.class }, inheritListeners = false)
172-
public static class AlwaysFailingAfterTestMethodTestCase extends AbstractTestNGSpringContextTests {
208+
@TestExecutionListeners(AlwaysFailingAfterTestClassTestExecutionListener.class)
209+
public static class AlwaysFailingAfterTestClassTestCase extends BaseTestCase {
210+
}
173211

174-
@org.testng.annotations.Test
175-
public void testNothing() {
176-
}
212+
@TestExecutionListeners(AlwaysFailingPrepareTestInstanceTestExecutionListener.class)
213+
public static class AlwaysFailingPrepareTestInstanceTestCase extends BaseTestCase {
177214
}
178215

216+
@TestExecutionListeners(AlwaysFailingBeforeTestMethodTestExecutionListener.class)
217+
public static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
218+
}
179219

180-
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
181-
public static class FailingBeforeTransactionalTestCase extends AbstractTransactionalTestNGSpringContextTests {
220+
@TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class)
221+
public static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {
222+
}
223+
224+
@ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml")
225+
public static class FailingBeforeTransactionTestCase extends AbstractTransactionalTestNGSpringContextTests {
182226

183227
@org.testng.annotations.Test
184228
public void testNothing() {
@@ -190,9 +234,8 @@ public void beforeTransaction() {
190234
}
191235
}
192236

193-
194-
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
195-
public static class FailingAfterTransactionalTestCase extends AbstractTransactionalTestNGSpringContextTests {
237+
@ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml")
238+
public static class FailingAfterTransactionTestCase extends AbstractTransactionalTestNGSpringContextTests {
196239

197240
@org.testng.annotations.Test
198241
public void testNothing() {

0 commit comments

Comments
 (0)