Skip to content
Paul Rogers edited this page Nov 28, 2016 · 4 revisions

Testing with JUnit

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.

References

JUnit/Hamcrest Idioms

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:

  • All tests are packaged into classes, all classes start or end with the word "Test". In Drill, most tests use the prefix format: "TestMumble".
  • Test methods are indicted with @Test.
  • Disabled tests are indicated with @Ignore("reason for ignoring")
  • Tests use "classic" JUnit assertions such as assertEquals(expected,actual,opt_msg).
  • Tests also use the newer "Hamcrest" 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.
  • Many tests make use of the test fixture annotations. These include methods marked to run before or after all tests in a class (@BeforeClass and @AfterClass) and those that run before or after each test (@Before and @After).
  • The base DrillTest class uses the ExceptionRule to declare that no test should throw an exception.
  • Some Drill tests verify exceptions directly using the expected parameter of @Test:
  @Test(expected = ExpressionParsingException.class)
  public void testSomething( ) {
  • Other code uses the try/catch idiom.
  • Drill tests have the potential to run for a long time, or hang, if thing go wrong. To prevent this, Drill tests use a timeout. The main Drill test base class, DrillTest uses a timeout rule to set a default timeout of 50 seconds:
@Rule public final TestRule TIMEOUT = TestTools.getTimeoutRule(50000);
  • Individual tests (override?) this rule with the timeout parameter to the Test annotation @Test(timeout=1000). This form an only decrease (but not increase) the timeout set by the timeout rule.
  • Tests that need a temporary file system folder use the @TemporaryFolder rule.
  • The base DrillTest class uses the TestName rule to make the current test name available to code: System.out.println( TEST_NAME );.

Additional Resources

Some other resources that may be of interest moving forward:

  • JUnitParams - a cleaner way to parameterize tests.
  • Assumptions for declaring dependencies and environment setup that a test assumes.
  • JUnit Rules may occasionally be helpful for specialized tests.
  • Categories to, perhaps, identify those "smoke" tests that should be run frequently, and a larger, more costly set of "full" tests to be run before commits, etc.
  • [System Rules][http://stefanbirkner.github.io/system-rules/] - A collection of JUnit rules for testing code that uses java.lang.System such as printing to System.out, environment variables, etc.
  • The Stopwatch rule added in JUnit 4.12 to measure the time a test takes.
  • the DisableonDebug rule added in JUnit 4.12 which can turn off other rules when needed in a debug session (to prevent, say, timeouts, etc.)

JUnit with Eclipse

Using JUnit with Eclipse is trivial:

  • To run all tests in a class, select the class name (or ensure no text is selected) and use the context menu option Debug As... --> JUnit.
  • To run a single test, select the name of the test method, and invoke the same menu command.

Clone this wiki locally