Skip to content

Commit 6327b34

Browse files
committed
[SPR-5145] Improved test suite for SpringJUnit4ClassRunner: added specific unit tests for test timeouts, repeated tests, and expected exceptions.
1 parent 41423a9 commit 6327b34

File tree

6 files changed

+454
-94
lines changed

6 files changed

+454
-94
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import java.util.ArrayList;
22+
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runner.notification.RunNotifier;
26+
import org.junit.runners.JUnit4;
27+
import org.springframework.test.annotation.ExpectedException;
28+
import org.springframework.test.context.TestExecutionListeners;
29+
30+
/**
31+
* Verifies proper handling of the following in conjunction with the
32+
* {@link SpringJUnit4ClassRunner}:
33+
* <ul>
34+
* <li>JUnit's {@link Test#expected() &#064;Test(expected=...)}</li>
35+
* <li>Spring's {@link ExpectedException &#064;ExpectedException}</li>
36+
* </ul>
37+
*
38+
* @author Sam Brannen
39+
* @since 3.0
40+
*/
41+
@RunWith(JUnit4.class)
42+
public class ExpectedExceptionSpringRunnerTests {
43+
44+
@Test
45+
public void timedTests() throws Exception {
46+
Class<ExpectedExceptionSpringRunnerTestCase> testClass = ExpectedExceptionSpringRunnerTestCase.class;
47+
TrackingRunListener listener = new TrackingRunListener();
48+
RunNotifier notifier = new RunNotifier();
49+
notifier.addListener(listener);
50+
51+
new SpringJUnit4ClassRunner(testClass).run(notifier);
52+
assertEquals("Verifying number of failures for test class [" + testClass + "].", 1,
53+
listener.getTestFailureCount());
54+
assertEquals("Verifying number of tests started for test class [" + testClass + "].", 3,
55+
listener.getTestStartedCount());
56+
assertEquals("Verifying number of tests finished for test class [" + testClass + "].", 3,
57+
listener.getTestFinishedCount());
58+
}
59+
60+
61+
@RunWith(SpringJUnit4ClassRunner.class)
62+
@TestExecutionListeners( {})
63+
public static final class ExpectedExceptionSpringRunnerTestCase {
64+
65+
// Should Pass.
66+
@Test(expected = IndexOutOfBoundsException.class)
67+
public void verifyJUnitExpectedException() {
68+
new ArrayList<Object>().get(1);
69+
}
70+
71+
// Should Pass.
72+
@Test
73+
@ExpectedException(IndexOutOfBoundsException.class)
74+
public void verifySpringExpectedException() {
75+
new ArrayList<Object>().get(1);
76+
}
77+
78+
// Should Fail due to duplicate configuration.
79+
@Test(expected = IllegalStateException.class)
80+
@ExpectedException(IllegalStateException.class)
81+
public void verifyJUnitAndSpringExpectedException() {
82+
new ArrayList<Object>().get(1);
83+
}
84+
85+
}
86+
87+
}

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

Lines changed: 21 additions & 44 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.
@@ -17,18 +17,16 @@
1717
package org.springframework.test.context.junit4;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.fail;
2021

2122
import java.util.Arrays;
2223
import java.util.Collection;
2324

2425
import org.junit.Test;
2526
import org.junit.runner.RunWith;
26-
import org.junit.runner.notification.Failure;
27-
import org.junit.runner.notification.RunListener;
2827
import org.junit.runner.notification.RunNotifier;
2928
import org.junit.runners.Parameterized;
3029
import org.junit.runners.Parameterized.Parameters;
31-
3230
import org.springframework.test.context.ContextConfiguration;
3331
import org.springframework.test.context.TestContext;
3432
import org.springframework.test.context.TestExecutionListener;
@@ -39,15 +37,15 @@
3937

4038
/**
4139
* <p>
42-
* JUnit 4 based unit test for verifying that '<em>before</em>' and '<em>after</em>'
43-
* methods of {@link TestExecutionListener TestExecutionListeners} as well as
44-
* {@link BeforeTransaction @BeforeTransaction} and
45-
* {@link AfterTransaction @AfterTransaction} methods can fail a test in a JUnit
46-
* 4.4 environment, as requested in <a
40+
* JUnit 4 based unit test for verifying that '<em>before</em>' and '
41+
* <em>after</em>' methods of {@link TestExecutionListener
42+
* TestExecutionListeners} as well as {@link BeforeTransaction
43+
* &#064;BeforeTransaction} and {@link AfterTransaction &#064;AfterTransaction}
44+
* methods can fail a test in a JUnit 4.4 environment, as requested in <a
4745
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3960"
4846
* target="_blank">SPR-3960</a>.
4947
* </p>
50-
*
48+
*
5149
* @author Sam Brannen
5250
* @since 2.5
5351
*/
@@ -63,60 +61,42 @@ public FailingBeforeAndAfterMethodsTests(final Class<?> clazz) {
6361

6462
@Parameters
6563
public static Collection<Object[]> testData() {
66-
return Arrays.asList(new Object[][] {
67-
68-
{ AlwaysFailingBeforeTestMethodTestCase.class },
69-
70-
{ AlwaysFailingAfterTestMethodTestCase.class },
71-
72-
{ FailingBeforeTransactionalTestCase.class },
73-
74-
{ FailingAfterTransactionalTestCase.class }
75-
64+
return Arrays.asList(new Object[][] {//
65+
//
66+
{ AlwaysFailingBeforeTestMethodTestCase.class },//
67+
{ AlwaysFailingAfterTestMethodTestCase.class },//
68+
{ FailingBeforeTransactionalTestCase.class },//
69+
{ FailingAfterTransactionalTestCase.class } //
7670
});
7771
}
7872

7973
@Test
8074
public void runTestAndAssertCounters() throws Exception {
81-
final FailureTrackingRunListener failureTrackingRunListener = new FailureTrackingRunListener();
75+
final TrackingRunListener listener = new TrackingRunListener();
8276
final RunNotifier notifier = new RunNotifier();
83-
notifier.addListener(failureTrackingRunListener);
77+
notifier.addListener(listener);
8478

8579
new SpringJUnit4ClassRunner(this.clazz).run(notifier);
86-
assertEquals("Verifying number of failures for test class [" + this.clazz + "].", 1,
87-
failureTrackingRunListener.failureCount);
88-
}
89-
90-
91-
static class FailureTrackingRunListener extends RunListener {
92-
93-
int failureCount = 0;
94-
95-
96-
public void testFailure(Failure failure) throws Exception {
97-
this.failureCount++;
98-
}
80+
assertEquals("Verifying number of failures for test class [" + this.clazz + "].", 1, listener.getTestFailureCount());
9981
}
10082

10183

10284
static class AlwaysFailingBeforeTestMethodTestExecutionListener extends AbstractTestExecutionListener {
10385

10486
@Override
10587
public void beforeTestMethod(TestContext testContext) {
106-
org.junit.Assert.fail("always failing beforeTestMethod()");
88+
fail("always failing beforeTestMethod()");
10789
}
10890
}
10991

110-
11192
static class AlwaysFailingAfterTestMethodTestExecutionListener extends AbstractTestExecutionListener {
11293

11394
@Override
11495
public void afterTestMethod(TestContext testContext) {
115-
org.junit.Assert.fail("always failing afterTestMethod()");
96+
fail("always failing afterTestMethod()");
11697
}
11798
}
11899

119-
120100
@TestExecutionListeners(value = { AlwaysFailingBeforeTestMethodTestExecutionListener.class }, inheritListeners = false)
121101
public static class AlwaysFailingBeforeTestMethodTestCase extends AbstractJUnit4SpringContextTests {
122102

@@ -125,7 +105,6 @@ public void testNothing() {
125105
}
126106
}
127107

128-
129108
@TestExecutionListeners(value = { AlwaysFailingAfterTestMethodTestExecutionListener.class }, inheritListeners = false)
130109
public static class AlwaysFailingAfterTestMethodTestCase extends AbstractJUnit4SpringContextTests {
131110

@@ -134,7 +113,6 @@ public void testNothing() {
134113
}
135114
}
136115

137-
138116
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
139117
public static class FailingBeforeTransactionalTestCase extends AbstractTransactionalJUnit4SpringContextTests {
140118

@@ -144,11 +122,10 @@ public void testNothing() {
144122

145123
@BeforeTransaction
146124
public void beforeTransaction() {
147-
org.junit.Assert.fail("always failing beforeTransaction()");
125+
fail("always failing beforeTransaction()");
148126
}
149127
}
150128

151-
152129
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
153130
public static class FailingAfterTransactionalTestCase extends AbstractTransactionalJUnit4SpringContextTests {
154131

@@ -158,7 +135,7 @@ public void testNothing() {
158135

159136
@AfterTransaction
160137
public void afterTransaction() {
161-
org.junit.Assert.fail("always failing afterTransaction()");
138+
fail("always failing afterTransaction()");
162139
}
163140
}
164141

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import java.io.IOException;
22+
import java.util.Arrays;
23+
import java.util.Collection;
24+
import java.util.concurrent.atomic.AtomicInteger;
25+
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runner.notification.RunNotifier;
29+
import org.junit.runners.Parameterized;
30+
import org.junit.runners.Parameterized.Parameters;
31+
import org.springframework.test.annotation.Repeat;
32+
import org.springframework.test.annotation.Timed;
33+
import org.springframework.test.context.ContextConfiguration;
34+
import org.springframework.test.context.TestExecutionListeners;
35+
36+
/**
37+
* Verifies proper handling of the following in conjunction with the
38+
* {@link SpringJUnit4ClassRunner}:
39+
* <ul>
40+
* <li>Spring's {@link Repeat &#064;Repeat}</li>
41+
* <li>Spring's {@link Timed &#064;Timed}</li>
42+
* </ul>
43+
*
44+
* @author Sam Brannen
45+
* @since 3.0
46+
*/
47+
@RunWith(Parameterized.class)
48+
public class RepeatedSpringRunnerTests {
49+
50+
private static final AtomicInteger invocationCount = new AtomicInteger();
51+
52+
private final Class<? extends AbstractRepeatedTestCase> testClass;
53+
54+
private final int expectedFailureCount;
55+
56+
private final int expectedTestStartedCount;
57+
58+
private final int expectedTestFinishedCount;
59+
60+
private final int expectedInvocationCount;
61+
62+
63+
public RepeatedSpringRunnerTests(Class<? extends AbstractRepeatedTestCase> testClass, int expectedFailureCount,
64+
int expectedTestStartedCount, int expectedTestFinishedCount, int expectedInvocationCount) {
65+
this.testClass = testClass;
66+
this.expectedFailureCount = expectedFailureCount;
67+
this.expectedTestStartedCount = expectedTestStartedCount;
68+
this.expectedTestFinishedCount = expectedTestFinishedCount;
69+
this.expectedInvocationCount = expectedInvocationCount;
70+
}
71+
72+
@Parameters
73+
public static Collection<Object[]> repetitionData() {
74+
return Arrays.asList(new Object[][] {//
75+
//
76+
{ NonAnnotatedRepeatedTestCase.class, 0, 1, 1, 1 },//
77+
{ DefaultRepeatValueRepeatedTestCase.class, 0, 1, 1, 1 },//
78+
{ NegativeRepeatValueRepeatedTestCase.class, 0, 1, 1, 1 },//
79+
{ RepeatedFiveTimesRepeatedTestCase.class, 0, 1, 1, 5 } //
80+
});
81+
}
82+
83+
@Test
84+
public void assertRepetitions() throws Exception {
85+
TrackingRunListener listener = new TrackingRunListener();
86+
RunNotifier notifier = new RunNotifier();
87+
notifier.addListener(listener);
88+
invocationCount.set(0);
89+
90+
new SpringJUnit4ClassRunner(this.testClass).run(notifier);
91+
assertEquals("Verifying number of failures for test class [" + this.testClass + "].",
92+
this.expectedFailureCount, listener.getTestFailureCount());
93+
assertEquals("Verifying number of tests started for test class [" + this.testClass + "].",
94+
this.expectedTestStartedCount, listener.getTestStartedCount());
95+
assertEquals("Verifying number of tests finished for test class [" + this.testClass + "].",
96+
this.expectedTestFinishedCount, listener.getTestFinishedCount());
97+
assertEquals("Verifying number of invocations for test class [" + this.testClass + "].",
98+
this.expectedInvocationCount, invocationCount.get());
99+
}
100+
101+
102+
@RunWith(SpringJUnit4ClassRunner.class)
103+
@TestExecutionListeners( {})
104+
@ContextConfiguration(locations = {})
105+
public abstract static class AbstractRepeatedTestCase {
106+
107+
protected void incrementInvocationCount() throws IOException {
108+
invocationCount.incrementAndGet();
109+
}
110+
}
111+
112+
public static final class NonAnnotatedRepeatedTestCase extends AbstractRepeatedTestCase {
113+
114+
@Test
115+
@Timed(millis = 10000)
116+
public void testNonAnnotated() throws Exception {
117+
incrementInvocationCount();
118+
}
119+
}
120+
121+
public static final class DefaultRepeatValueRepeatedTestCase extends AbstractRepeatedTestCase {
122+
123+
@Test
124+
@Repeat
125+
@Timed(millis = 10000)
126+
public void testDefaultRepeatValue() throws Exception {
127+
incrementInvocationCount();
128+
}
129+
}
130+
131+
public static final class NegativeRepeatValueRepeatedTestCase extends AbstractRepeatedTestCase {
132+
133+
@Test
134+
@Repeat(-5)
135+
@Timed(millis = 10000)
136+
public void testNegativeRepeatValue() throws Exception {
137+
incrementInvocationCount();
138+
}
139+
}
140+
141+
public static final class RepeatedFiveTimesRepeatedTestCase extends AbstractRepeatedTestCase {
142+
143+
@Test
144+
@Repeat(5)
145+
@Timed(millis = 10000)
146+
public void testRepeatedFiveTimes() throws Exception {
147+
incrementInvocationCount();
148+
}
149+
}
150+
151+
}

0 commit comments

Comments
 (0)