Most of us know the basics of JUnit. Drill uses many advanced features that we mention here. Drill uses JUnit 4, currently version 4.11.
Drill tests use the JUnit 4 series that uses annotations to identify tests. Drill makes use of the “Hamcrest” additions (which seem to have come from a separate project, later merged into JUnit, hence the strange naming.) Basic rules:
@Test
.@Ignore("reason for ignoring")
assertEquals(expected,actual,opt_msg)
.assertThat
formulation. The Hamcrest project provided a system based on assertions and matchers that are quite handy for cases that are cumbersome with the JUnit-Style assertions.@BeforeClass
and @AfterClass
) and those that run before or after each test (@Before
and @After
).DrillTest
class uses the ExceptionRule
to declare that no test should throw an exception.expected
parameter of @Test
:@Test(expected = ExpressionParsingException.class) public void testSomething( ) {
DrillTest
uses a timeout rule to set a default timeout of 50 seconds:@Rule public final TestRule TIMEOUT = TestTools.getTimeoutRule(50000);
@Test(timeout=1000)
. This form an only decrease (but not increase) the timeout set by the timeout rule.@TemporaryFolder
rule.DrillTest
class uses the TestName
rule to make the current test name available to code: System.out.println( TEST_NAME );
.Some other resources that may be of interest moving forward:
java.lang.System
such as printing to System.out
, environment variables, etc.Stopwatch
rule added in JUnit 4.12 to measure the time a test takes.DisableonDebug
rule added in JUnit 4.12 which can turn off other rules when needed in a debug session (to prevent, say, timeouts, etc.)The root Drill pom.xml
declares a test-time dependency on JUnit 4.11:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>
Since this dependency is in the root POM, there is no need to add it to the POM files of each Drill module.
Using JUnit with Eclipse is trivial:
It is necessary to have Eclipse run on the same version of Java as Drill.
To use Java 8:
-vm /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/bin