1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .test .context .junit4 ;
18
18
19
- import java .util .Arrays ;
20
- import java .util .Collection ;
21
-
22
19
import org .junit .Test ;
23
20
import org .junit .runner .RunWith ;
24
21
import org .junit .runners .Parameterized ;
28
25
import org .springframework .test .context .TestContext ;
29
26
import org .springframework .test .context .TestExecutionListener ;
30
27
import org .springframework .test .context .TestExecutionListeners ;
31
- import org .springframework .test .context .support .AbstractTestExecutionListener ;
32
28
import org .springframework .test .context .testng .AbstractTestNGSpringContextTests ;
33
29
import org .springframework .test .context .testng .AbstractTransactionalTestNGSpringContextTests ;
34
30
import org .springframework .test .context .testng .TrackingTestNGTestListener ;
41
37
import static org .junit .Assert .*;
42
38
43
39
/**
44
- * <p>
45
- * JUnit 4 based integration test for verifying that '<i>before</i>' and '<i>after</i>'
40
+ * Integration tests which verify that '<i>before</i>' and '<i>after</i>'
46
41
* methods of {@link TestExecutionListener TestExecutionListeners} as well as
47
- * {@link BeforeTransaction @BeforeTransaction} and
48
- * {@link AfterTransaction @AfterTransaction} methods can fail a test in a
49
- * TestNG environment, as requested in <a
50
- * href="http://opensource.atlassian.com/projects/spring/browse/SPR-3960"
51
- * target="_blank">SPR-3960</a>.
52
- * </p>
53
- * <p>
54
- * Indirectly, this class also verifies that all {@link TestExecutionListener}
42
+ * {@code @BeforeTransaction} and {@code @AfterTransaction} methods can fail
43
+ * tests in a TestNG environment.
44
+ *
45
+ * <p>See: <a href="https://jira.spring.io/browse/SPR-3960" target="_blank">SPR-3960</a>.
46
+ *
47
+ * <p>Indirectly, this class also verifies that all {@code TestExecutionListener}
55
48
* lifecycle callbacks are called.
56
- * </p>
57
- * <p>
58
- * As of Spring 3.0, this class also tests support for the new
59
- * {@link TestExecutionListener#beforeTestClass(TestContext) beforeTestClass()}
60
- * and {@link TestExecutionListener#afterTestClass(TestContext)
61
- * afterTestClass()} lifecycle callback methods.
62
- * </p>
63
49
*
64
50
* @author Sam Brannen
65
51
* @since 2.5
@@ -75,17 +61,20 @@ public class FailingBeforeAndAfterMethodsTestNGTests {
75
61
76
62
77
63
@ Parameters (name = "{0}" )
78
- public static Collection <Object []> testData () {
79
- return Arrays .asList (new Object [][] {//
80
- //
81
- { AlwaysFailingBeforeTestClassTestCase .class .getSimpleName (), 1 , 0 , 0 , 1 },//
82
- { AlwaysFailingAfterTestClassTestCase .class .getSimpleName (), 1 , 1 , 0 , 1 },//
83
- { AlwaysFailingPrepareTestInstanceTestCase .class .getSimpleName (), 1 , 0 , 0 , 1 },//
84
- { AlwaysFailingBeforeTestMethodTestCase .class .getSimpleName (), 1 , 0 , 0 , 1 },//
85
- { AlwaysFailingAfterTestMethodTestCase .class .getSimpleName (), 1 , 1 , 0 , 1 },//
86
- { FailingBeforeTransactionTestCase .class .getSimpleName (), 1 , 0 , 0 , 1 },//
87
- { FailingAfterTransactionTestCase .class .getSimpleName (), 1 , 1 , 0 , 1 } //
88
- });
64
+ public static Object [][] testData () {
65
+ // @formatter:off
66
+ return new Object [][] {
67
+ { AlwaysFailingBeforeTestClassTestCase .class .getSimpleName (), 1 , 0 , 0 , 1 },
68
+ { AlwaysFailingAfterTestClassTestCase .class .getSimpleName (), 1 , 1 , 0 , 1 },
69
+ { AlwaysFailingPrepareTestInstanceTestCase .class .getSimpleName (), 1 , 0 , 0 , 1 },
70
+ { AlwaysFailingBeforeTestMethodTestCase .class .getSimpleName (), 1 , 0 , 0 , 1 },
71
+ { AlwaysFailingBeforeTestExecutionTestCase .class .getSimpleName (), 1 , 0 , 1 , 0 },
72
+ { AlwaysFailingAfterTestExecutionTestCase .class .getSimpleName (), 1 , 0 , 1 , 0 },
73
+ { AlwaysFailingAfterTestMethodTestCase .class .getSimpleName (), 1 , 1 , 0 , 1 },
74
+ { FailingBeforeTransactionTestCase .class .getSimpleName (), 1 , 0 , 0 , 1 },
75
+ { FailingAfterTransactionTestCase .class .getSimpleName (), 1 , 1 , 0 , 1 }
76
+ };
77
+ // @formatter:on
89
78
}
90
79
91
80
public FailingBeforeAndAfterMethodsTestNGTests (String testClassName , int expectedTestStartCount ,
@@ -106,51 +95,67 @@ public void runTestAndAssertCounters() throws Exception {
106
95
testNG .setVerbose (0 );
107
96
testNG .run ();
108
97
109
- assertEquals ( "Verifying number of test starts for test class [" + this .clazz + "]." ,
110
- this . expectedTestStartCount , listener . testStartCount );
111
- assertEquals ("Verifying number of successful tests for test class [" + this . clazz + "]." ,
112
- this .expectedTestSuccessCount , listener . testSuccessCount );
113
- assertEquals ( "Verifying number of failures for test class [" + this . clazz + "]." , this . expectedFailureCount ,
114
- listener .testFailureCount );
115
- assertEquals ("Verifying number of failed configurations for test class [" + this . clazz + "]." ,
116
- this . expectedFailedConfigurationsCount , listener .failedConfigurationsCount );
98
+ String name = this .clazz . getSimpleName ();
99
+
100
+ assertEquals ("tests started for [" + name + "] ==> " , this . expectedTestStartCount , listener . testStartCount );
101
+ assertEquals ( "successful tests for [" + name + "] ==> " , this .expectedTestSuccessCount ,
102
+ listener . testSuccessCount );
103
+ assertEquals ( "failed tests for [" + name + "] ==> " , this . expectedFailureCount , listener .testFailureCount );
104
+ assertEquals ("failed configurations for [" + name + "] ==> " , this . expectedFailedConfigurationsCount ,
105
+ listener .failedConfigurationsCount );
117
106
}
118
107
119
108
// -------------------------------------------------------------------
120
109
121
- static class AlwaysFailingBeforeTestClassTestExecutionListener extends AbstractTestExecutionListener {
110
+ static class AlwaysFailingBeforeTestClassTestExecutionListener implements TestExecutionListener {
122
111
123
112
@ Override
124
113
public void beforeTestClass (TestContext testContext ) {
125
114
org .testng .Assert .fail ("always failing beforeTestClass()" );
126
115
}
127
116
}
128
117
129
- static class AlwaysFailingAfterTestClassTestExecutionListener extends AbstractTestExecutionListener {
118
+ static class AlwaysFailingAfterTestClassTestExecutionListener implements TestExecutionListener {
130
119
131
120
@ Override
132
121
public void afterTestClass (TestContext testContext ) {
133
122
org .testng .Assert .fail ("always failing afterTestClass()" );
134
123
}
135
124
}
136
125
137
- static class AlwaysFailingPrepareTestInstanceTestExecutionListener extends AbstractTestExecutionListener {
126
+ static class AlwaysFailingPrepareTestInstanceTestExecutionListener implements TestExecutionListener {
138
127
139
128
@ Override
140
129
public void prepareTestInstance (TestContext testContext ) throws Exception {
141
130
org .testng .Assert .fail ("always failing prepareTestInstance()" );
142
131
}
143
132
}
144
133
145
- static class AlwaysFailingBeforeTestMethodTestExecutionListener extends AbstractTestExecutionListener {
134
+ static class AlwaysFailingBeforeTestMethodTestExecutionListener implements TestExecutionListener {
146
135
147
136
@ Override
148
137
public void beforeTestMethod (TestContext testContext ) {
149
138
org .testng .Assert .fail ("always failing beforeTestMethod()" );
150
139
}
151
140
}
152
141
153
- static class AlwaysFailingAfterTestMethodTestExecutionListener extends AbstractTestExecutionListener {
142
+ static class AlwaysFailingBeforeTestExecutionTestExecutionListener implements TestExecutionListener {
143
+
144
+ @ Override
145
+ public void beforeTestExecution (TestContext testContext ) {
146
+ org .testng .Assert .fail ("always failing beforeTestExecution()" );
147
+ }
148
+ }
149
+
150
+ static class AlwaysFailingAfterTestExecutionTestExecutionListener implements TestExecutionListener {
151
+
152
+ @ Override
153
+ public void afterTestExecution (TestContext testContext ) {
154
+ org .testng .Assert .fail ("always failing afterTestExecution()" );
155
+ }
156
+ }
157
+
158
+ static class AlwaysFailingAfterTestMethodTestExecutionListener implements TestExecutionListener {
154
159
155
160
@ Override
156
161
public void afterTestMethod (TestContext testContext ) {
@@ -160,7 +165,7 @@ public void afterTestMethod(TestContext testContext) {
160
165
161
166
// -------------------------------------------------------------------
162
167
163
- @ TestExecutionListeners (value = {}, inheritListeners = false )
168
+ @ TestExecutionListeners (inheritListeners = false )
164
169
public static abstract class BaseTestCase extends AbstractTestNGSpringContextTests {
165
170
166
171
@ org .testng .annotations .Test
@@ -184,6 +189,14 @@ public static class AlwaysFailingPrepareTestInstanceTestCase extends BaseTestCas
184
189
public static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
185
190
}
186
191
192
+ @ TestExecutionListeners (AlwaysFailingBeforeTestExecutionTestExecutionListener .class )
193
+ public static class AlwaysFailingBeforeTestExecutionTestCase extends BaseTestCase {
194
+ }
195
+
196
+ @ TestExecutionListeners (AlwaysFailingAfterTestExecutionTestExecutionListener .class )
197
+ public static class AlwaysFailingAfterTestExecutionTestCase extends BaseTestCase {
198
+ }
199
+
187
200
@ TestExecutionListeners (AlwaysFailingAfterTestMethodTestExecutionListener .class )
188
201
public static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {
189
202
}
0 commit comments