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.
18
18
19
19
import java .lang .reflect .Constructor ;
20
20
21
+ import org .junit .experimental .ParallelComputer ;
22
+ import org .junit .jupiter .api .Assertions ;
23
+ import org .junit .runner .Computer ;
21
24
import org .junit .runner .JUnitCore ;
22
25
import org .junit .runner .RunWith ;
23
26
import org .junit .runner .Runner ;
24
27
import org .junit .runner .notification .RunNotifier ;
25
28
26
29
import org .springframework .beans .BeanUtils ;
27
30
28
- import static org .junit .Assert .*;
31
+ import static org .junit .jupiter .api .Assertions .assertAll ;
32
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
29
33
30
34
/**
31
- * Collection of utilities for testing the execution of JUnit tests.
35
+ * Collection of utilities for testing the execution of JUnit 4 based tests.
36
+ *
37
+ * <p>Note that these utilities use {@link Assertions} from JUnit Jupiter,
38
+ * but that should not result in any adverse side effects in terms of
39
+ * proper test failure for failed assertions.
32
40
*
33
41
* @author Sam Brannen
34
42
* @since 4.2
@@ -38,8 +46,8 @@ public class JUnitTestingUtils {
38
46
39
47
/**
40
48
* Run the tests in the supplied {@code testClass}, using the {@link Runner}
41
- * it is configured with (i.e., via {@link RunWith @RunWith}) or the default
42
- * JUnit runner, and assert the expectations of the test execution.
49
+ * configured via {@link RunWith @RunWith} or the default JUnit runner, and
50
+ * assert the expectations of the test execution.
43
51
*
44
52
* @param testClass the test class to run with JUnit
45
53
* @param expectedStartedCount the expected number of tests that started
@@ -65,7 +73,7 @@ public static void runTestsAndAssertCounters(Class<?> testClass, int expectedSta
65
73
* (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
66
74
*
67
75
* @param runnerClass the explicit runner class to use or {@code null}
68
- * if the implicit runner should be used
76
+ * if the default JUnit runner should be used
69
77
* @param testClass the test class to run with JUnit
70
78
* @param expectedStartedCount the expected number of tests that started
71
79
* @param expectedFailedCount the expected number of tests that failed
@@ -93,11 +101,54 @@ public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass
93
101
junit .run (testClass );
94
102
}
95
103
96
- assertEquals ("tests started for [" + testClass + "]:" , expectedStartedCount , listener .getTestStartedCount ());
97
- assertEquals ("tests failed for [" + testClass + "]:" , expectedFailedCount , listener .getTestFailureCount ());
98
- assertEquals ("tests finished for [" + testClass + "]:" , expectedFinishedCount , listener .getTestFinishedCount ());
99
- assertEquals ("tests ignored for [" + testClass + "]:" , expectedIgnoredCount , listener .getTestIgnoredCount ());
100
- assertEquals ("failed assumptions for [" + testClass + "]:" , expectedAssumptionFailedCount , listener .getTestAssumptionFailureCount ());
104
+ // @formatter:off
105
+ assertAll (
106
+ () -> assertEquals (expectedStartedCount , listener .getTestStartedCount (), "tests started for [" + testClass + "]" ),
107
+ () -> assertEquals (expectedFailedCount , listener .getTestFailureCount (), "tests failed for [" + testClass + "]" ),
108
+ () -> assertEquals (expectedFinishedCount , listener .getTestFinishedCount (), "tests finished for [" + testClass + "]" ),
109
+ () -> assertEquals (expectedIgnoredCount , listener .getTestIgnoredCount (), "tests ignored for [" + testClass + "]" ),
110
+ () -> assertEquals (expectedAssumptionFailedCount , listener .getTestAssumptionFailureCount (), "failed assumptions for [" + testClass + "]" )
111
+ );
112
+ // @formatter:on
113
+ }
114
+
115
+ /**
116
+ * Run all tests in the supplied test classes according to the policies of
117
+ * the supplied {@link Computer}, using the {@link Runner} configured via
118
+ * {@link RunWith @RunWith} or the default JUnit runner, and assert the
119
+ * expectations of the test execution.
120
+ *
121
+ * <p>To have all tests executed in parallel, supply {@link ParallelComputer#methods()}
122
+ * as the {@code Computer}. To have all tests executed serially, supply
123
+ * {@link Computer#serial()} as the {@code Computer}.
124
+ *
125
+ * @param computer the JUnit {@code Computer} to use
126
+ * @param expectedStartedCount the expected number of tests that started
127
+ * @param expectedFailedCount the expected number of tests that failed
128
+ * @param expectedFinishedCount the expected number of tests that finished
129
+ * @param expectedIgnoredCount the expected number of tests that were ignored
130
+ * @param expectedAssumptionFailedCount the expected number of tests that
131
+ * resulted in a failed assumption
132
+ * @param testClasses one or more test classes to run
133
+ */
134
+ public static void runTestsAndAssertCounters (Computer computer , int expectedStartedCount , int expectedFailedCount ,
135
+ int expectedFinishedCount , int expectedIgnoredCount , int expectedAssumptionFailedCount ,
136
+ Class <?>... testClasses ) throws Exception {
137
+
138
+ JUnitCore junit = new JUnitCore ();
139
+ TrackingRunListener listener = new TrackingRunListener ();
140
+ junit .addListener (listener );
141
+ junit .run (computer , testClasses );
142
+
143
+ // @formatter:off
144
+ assertAll (
145
+ () -> assertEquals (expectedStartedCount , listener .getTestStartedCount (), "tests started" ),
146
+ () -> assertEquals (expectedFailedCount , listener .getTestFailureCount (), "tests failed" ),
147
+ () -> assertEquals (expectedFinishedCount , listener .getTestFinishedCount (), "tests finished" ),
148
+ () -> assertEquals (expectedIgnoredCount , listener .getTestIgnoredCount (), "tests ignored" ),
149
+ () -> assertEquals (expectedAssumptionFailedCount , listener .getTestAssumptionFailureCount (), "failed assumptions" )
150
+ );
151
+ // @formatter:on
101
152
}
102
153
103
154
}
0 commit comments