11/*
2- * Copyright 2002-2015 the original author or authors.
2+ * Copyright 2002-2016 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.
1818
1919import java .lang .reflect .Constructor ;
2020
21+ import org .junit .experimental .ParallelComputer ;
22+ import org .junit .jupiter .api .Assertions ;
23+ import org .junit .runner .Computer ;
2124import org .junit .runner .JUnitCore ;
2225import org .junit .runner .RunWith ;
2326import org .junit .runner .Runner ;
2427import org .junit .runner .notification .RunNotifier ;
2528
2629import org .springframework .beans .BeanUtils ;
2730
28- import static org .junit .Assert .*;
31+ import static org .junit .jupiter .api .Assertions .assertAll ;
32+ import static org .junit .jupiter .api .Assertions .assertEquals ;
2933
3034/**
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.
3240 *
3341 * @author Sam Brannen
3442 * @since 4.2
@@ -38,8 +46,8 @@ public class JUnitTestingUtils {
3846
3947 /**
4048 * 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.
4351 *
4452 * @param testClass the test class to run with JUnit
4553 * @param expectedStartedCount the expected number of tests that started
@@ -65,7 +73,7 @@ public static void runTestsAndAssertCounters(Class<?> testClass, int expectedSta
6573 * (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
6674 *
6775 * @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
6977 * @param testClass the test class to run with JUnit
7078 * @param expectedStartedCount the expected number of tests that started
7179 * @param expectedFailedCount the expected number of tests that failed
@@ -93,11 +101,54 @@ public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass
93101 junit .run (testClass );
94102 }
95103
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
101152 }
102153
103154}
0 commit comments