JavaOne: JUnit 5 Features, Architecture and Extensibility

“JUnit 5 Features, Architecture and Extensibility”

Speaker: Steve Moyer, Stephen Seltzer & Niraja Ramesh
Deck on GitHub

For more blog posts from JavaOne, see the table of contents


This is “part 2” because last year they gave a session. It’s an expanded version of that talk now that JUnit 5 is done.

Rationale

  • Decouple test execution/reporting from test definition/provisioning
  • JUnit 8 features. ex: message nw last param and can be lambda
  • migration-support – JUnit 4 rules
  • junit-platform-console – to execute from command line; have to build full classpath

Principles

  • Prefer extension points over features
  • An extension point shoul be good at one thing
  • Should be hard to write tests that behave differently based on how run
  • Test should be easy to understand
  • Minimize dependencies; epecially third party

Features

  • Meta Annotations – compose annotations out of other annotations
  • Tags/filters
  • Dependency injection through params on constructors/methods
  • Default interface methods

Code examples (also on GitHub

  • Assertions – assertEquals(expected, actual, message)
  • Skip tests @Disabled
  • Lambdas – assertTrue(a, () -> message)
  • Grouped assertions – assertAll(message, () -> assertEquals(expected, actual, essage))
  • Exceptions – assertThrows
  • Assumptions assumeTrue, assumeFalse, assumingThat – he last one sets a scope (lambda) for the assumption and continues after that block regardless
  • Meta-annotations Create annotation with other annotation. Similarly can tag tests with annotations. Eclipse lets you specify filters in run config
  • Repeated tests – pass how many times to run along with what to display – can pass current repetition an tota reptitions to display name. Not for performance testing (other tools for that) but good for idempotence testing
  • Parameterized tests – can feed in CSVFileSource, CSVSource (CSV as strings), MethodSource. See docs for implicit conversions [didn’t mention value source :(]
  • Test templates – designed to be invoked multiple times and run like indepenendent test method. Parameterized tests are an implmentation of test templates
  • Lots of extension points and callbacks. Can programmatially decide whether to run tests.
  • ParameterResolvers – TestInfo (has display name, test class and test method), TestReporter (allows publising to output report) – can have JUnit pass as param to test
  • Callback order – BeforeAll, BeforeEach, BeforeTestExecution, Test, AfterTestExcution, AfterEach, AfterAll. Does parent class before child on Before*** and after child on After***
  • Dynamic tests – use @TestFactory – have display name and Executable interface
  • Grouped tests – Can use traditional for loop, assertAll or factory tests. Different output. Traditional for loop only ells you about first failure. With assertAll, get all failures but one faling test. With factory tests, you have a failing test for each faiure.

Future