The JUnit Toolbox provides some useful classes for writing automated tests with JUnit:
- MultithreadingTester -- Helper class for writing stress tests using multiple, concurrently running threads
- PollingWait -- Helper class to wait for asynchronous operations
- ParallelRunner -- Executes all
@Testmethods as well as the calls to@Theorymethods with different parameter assignments concurrently using several worker threads. - ParallelParameterized -- A replacement for the JUnit runner
Parameterizedwhich executes the tests for each parameter set concurrently. - WildcardPatternSuite -- A replacement for the JUnit runners
SuiteandCategories, which allows you to specify the children classes of your test suite class using a wildcard pattern. Furthermore you can include and/or exclude multiple categories. - ParallelSuite -- An extension of the
WildcardPatternSuite, which executes its children classes concurrently using several worker threads. Although it extendsWildcardPatternSuiteyou are not forced to use a wildcard pattern, you can also list the children class using the@SuiteClassesannotation known from JUnit. - InnerTestClassesSuite -- A replacement for the JUnit runner
Enclosedwhich executes all inner test classes of the class annotated with@RunWith(InnerTestClassesSuite.class). In contrast to theEnclosedrunner provided by JUnit it detects if an inner class is actually a test class and ignores all other inner classes.
ParallelRunner, ParallelParameterized, and ParallelSuite share a common Fork-Join-Pool. You can control the maximum number of worker threads by specifying the system property maxParallelTestThreads. If this system property is not set, there will be as many worker threads as the number of processors available to the JVM.
If you use Maven, add the following dependency to your pom.xml file:
<dependency>
<groupId>com.googlecode.junit-toolbox</groupId>
<artifactId>junit-toolbox</artifactId>
<version>2.2</version>
</dependency>
- Updated to JUnit 4.12
- Improved WildcardPatternSuite: it now ignores classes, which match the specified wildcard pattern, but are not test classes (fixes issue #8)
- Added the ParallelParameterized runner contributed by Stefan Birkner.
- Upgraded to Java 8. Note: This does not mean, that there won't be any new version for Java 6. If a new feature or bug fix (which is compatible to Java 6) is added to the code base, I will release a new 1.x version as well as a new 2.x version.
- Added overloaded
untilmethod toPollingWaitwhich takes aCallable<Boolean>as parameter, which allows to use lambda expressions or method references. Example:
private PollingWait wait = new PollingWait().timeoutAfter(5, SECONDS)
.pollEvery(100, MILLISECONDS);
@Test
public void test_login() throws Exception {
// ... enter credentials into login form ...
clickOnButton("Login");
wait.until(() -> webDriver.findElement(By.linkText("Logout")).isDisplayed());
// ...
}
protected void clickOnButton(String label) {
WebElement button = findButton(label);
wait.until(button::isDisplayed);
button.click();
}
- Fixed bug in
MultithreadingTesterintroduced in version 1.5
- Added annotations
@IncludeCategoriesand@ExcludeCategories. In contrast to JUnit 4, which only offers the annotations@IncludeCategoryand@ExcludeCategorywhich allow to specify a single category, these new annotations allow you to specify multiple categories. The annotations can be used withWildcardPatternSuiteand withParallelSuite. Example:
@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
@ExcludeCategories({SlowTests.class, FlakyTests.class})
public class NormalTests {}
- Minor bugfixes in
PollingWaitclass
- Added deadlock detection to
MultithreadingTester
- Added new utility class
MultithreadingTester - The
SuiteClassesannotation accepts multiple wildcard patterns now as well as negated patterns. E.g.
@RunWith(WildcardPatternSuite.class)
@SuiteClasses({"**/*Test.class", "!samples/**"})
public class AllTests {}
- Added
PollingWaitandRunnableAssertfor waiting that an asynchronous operation succeeds. Unlike theWebDriverWaitclass provided by Selenium this class does not wait for an artificial condition, which might be wrong and can make your test non-deterministic, this class waits until your assertions become true (or a configurable timeout is reached). Example:
private PollingWait wait = new PollingWait().timeoutAfter(5, SECONDS).pollEvery(100, MILLISECONDS);
@Test
public void test_auto_complete() throws Exception {
// Enter "cheese" into auto complete field ...
...
wait.until(new RunnableAssert("'cheesecake' is displayed in auto-complete <div>") { @Override public void run() throws Exception {
WebElement autoCompleteDiv = driver.findElement(By.id("auto-complete"));
assertThat(autoCompleteDiv, isVisible());
assertThat(autoCompleteDiv, containsText("cheesecake"));
}});
}
ParallelRunnerextends theTheoriesrunner provided by JUnit now, and can be used as a replacement for it. It still executes all normal@Testmethods concurrently. Furthermore it executes the calls to@Theorymethods with different parameter assignments concurrently too.
- The
WildcardPatternSuiterunner supports the annotations@IncludeCategoryand@ExcludeCategorynow and can therefore be used as a replacement for theCategoriesrunner provided by JUnit. Example:
@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
@IncludeCategory(SlowTests.class)
public class OnlySlowTests {}
- New runner
InnerTestClassesSuitewhich runs all inner test classes of the class annotated with@RunWith(InnerTestClassesSuite.class). In contrast to theEnclosedrunner provided by JUnit, it detects if an inner class is actually a test class and ignores all other inner classes. Example:
@RunWith(InnerTestClassesSuite.class)
public class LoginBeanTests {
public static class UnitTests {
@Test
public void test1() { ... }
}
@Configuration
public static class IntegrationTestsConfig { ... }
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = IntegrationTestsConfig.class)
public static class IntegrationTests {
@Test
public void test2() { ... }
}
}
- bundled with JUnit 4.10, Hamcrest 1.3, and Mockito 1.9.5
WildcardPatternSuiterunner for specifying the children classes of a test suite with a wildcard pattern like this:
@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
public class AllTests {}
ParallelSuitefor concurrent execution of test classes. You can either list the test classes, if you use the@SuiteClassesannotation provided by JUnit itself, for example:
@RunWith(ParallelSuite.class)
@SuiteClasses({
LoginFrontendTest.class,
FillOutFormFrontendTest.class,
...
})
public class AllFrontendTests {}
or you can use a wildcard pattern if you use the
@SuiteClassesannotation of JUnit Toolbox:
@RunWith(ParallelSuite.class)
@SuiteClasses("**/*FrontendTest.class")
public class AllFrontendTests {}
ParallelRunnerfor concurrent execution of the test methods in a test class. Example:
@RunWith(ParallelRunner.class)
public class FooTest {
@Test
public void test1() {
// Will be executed in a worker thread
}
@Test
public void test2() {
// Will be executed concurrently in another worker thread
}
}
ParallelSuiteandParallelRunnershare a common Fork-Join-Pool (to be compatible with Java 6, the JSR-166y code is used). You can control the maximum number of worker threads by specifying the system propertymaxParallelTestThreads. If this system property is not set, there will be as many worker threads as the number of processors available to the JVM.