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
@Test
methods as well as the calls to@Theory
methods with different parameter assignments concurrently using several worker threads. - ParallelParameterized -- A replacement for the JUnit runner
Parameterized
which executes the tests for each parameter set concurrently. - WildcardPatternSuite -- A replacement for the JUnit runners
Suite
andCategories
, 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 extendsWildcardPatternSuite
you are not forced to use a wildcard pattern, you can also list the children class using the@SuiteClasses
annotation known from JUnit. - InnerTestClassesSuite -- A replacement for the JUnit runner
Enclosed
which executes all inner test classes of the class annotated with@RunWith(InnerTestClassesSuite.class)
. In contrast to theEnclosed
runner 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
until
method toPollingWait
which 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
MultithreadingTester
introduced in version 1.5
- Added annotations
@IncludeCategories
and@ExcludeCategories
. In contrast to JUnit 4, which only offers the annotations@IncludeCategory
and@ExcludeCategory
which allow to specify a single category, these new annotations allow you to specify multiple categories. The annotations can be used withWildcardPatternSuite
and withParallelSuite
. Example:
@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
@ExcludeCategories({SlowTests.class, FlakyTests.class})
public class NormalTests {}
- Minor bugfixes in
PollingWait
class
- Added deadlock detection to
MultithreadingTester
- Added new utility class
MultithreadingTester
- The
SuiteClasses
annotation accepts multiple wildcard patterns now as well as negated patterns. E.g.
@RunWith(WildcardPatternSuite.class)
@SuiteClasses({"**/*Test.class", "!samples/**"})
public class AllTests {}
- Added
PollingWait
andRunnableAssert
for waiting that an asynchronous operation succeeds. Unlike theWebDriverWait
class 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"));
}});
}
ParallelRunner
extends theTheories
runner provided by JUnit now, and can be used as a replacement for it. It still executes all normal@Test
methods concurrently. Furthermore it executes the calls to@Theory
methods with different parameter assignments concurrently too.
- The
WildcardPatternSuite
runner supports the annotations@IncludeCategory
and@ExcludeCategory
now and can therefore be used as a replacement for theCategories
runner provided by JUnit. Example:
@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
@IncludeCategory(SlowTests.class)
public class OnlySlowTests {}
- New runner
InnerTestClassesSuite
which runs all inner test classes of the class annotated with@RunWith(InnerTestClassesSuite.class)
. In contrast to theEnclosed
runner 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
WildcardPatternSuite
runner for specifying the children classes of a test suite with a wildcard pattern like this:
@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
public class AllTests {}
ParallelSuite
for concurrent execution of test classes. You can either list the test classes, if you use the@SuiteClasses
annotation 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
@SuiteClasses
annotation of JUnit Toolbox:
@RunWith(ParallelSuite.class)
@SuiteClasses("**/*FrontendTest.class")
public class AllFrontendTests {}
ParallelRunner
for 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
}
}
ParallelSuite
andParallelRunner
share 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.