Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The JUnit Crew Presents What's New

The JUnit Crew Presents What's New

In this session, the JUnit team will introduce the recent 5.11 release’s new features, as well as the highlights of the previous 5.9 and 5.10 releases.

For JUnit Jupiter, their demos will include enhancements made to Jupiter core extensions (for example, @AutoClose, @TempDir, and @Timeout), parameterized tests (for example, @FieldSource and convenience base classes), repeated tests (failure threshold), and the extension model (for example, ExecutableInvoker and the new TestInstancePreConstructCallback extension point).

On the platform side, the team will demonstrate life cycle methods for test suites, new ConsoleLauncher options, and the dry-run mode for test execution.

Last but not least, they will share exciting news about their agreement with the Sovereign Tech Fund, who have planned to invest in the project throughout 2024 and 2025. They will conclude the session with an outlook of what’s coming in JUnit 5.12.

Marc Philipp

November 27, 2024
Tweet

More Decks by Marc Philipp

Other Decks in Programming

Transcript

  1. Adoption • Maven Central downloads as of 9/2024: ◦ JUnit

    Jupiter: 50 million downloads per month ◦ JUnit 4: 45 million downloads per month ◦ JUnit 3: 27 million downloads per month ◦ JUnit 5 BOM: 129 million downloads per month • New projects (e.g. Spring Initializr, Gradle init task) use JUnit Platform with Jupiter by default • JUnit 5 is used by 477k repositories on GitHub 5
  2. Console & Launcher • The Launcher now provides a --version

    option • The existing functionality of the Launcher has been split into two subcommands: execute for executing tests and engines for listing registered test engines • A new discover subcommand has been added to the Launcher to print the discovered tests for the specified details mode without executing them • New dry-run execution mode to simulate test execution without actually running tests 9
  3. Platform News • Stack traces produced by failing tests are

    now pruned of calls from the org.junit, jdk.internal.reflect, and other packages • New ConversionSupport utility in junit-platform-commons which exposes the conversion logic that was previously private — for re-use in third-party extensions and test engines • Support for the Open Test Reporting format which supports all features of the JUnit Platform such as hierarchical test structures, display names, tags, etc. • JUnit Platform Standalone Console JAR now also includes the JUnit Platform Suite Engine 10
  4. Test Instance Pre-construct Callback 13 • Extension invoked prior to

    test instances being constructed • Symmetric call to TestInstancePreDestroyCallback
  5. Executable Invoker 14 • Allows invoking methods and constructors with

    support for dynamic resolution of parameters via ParameterResolvers • Available via ExtensionContext.getExecutableInvoker() Leveraged by: • Parameterized Test Extension (@MethodSource) • Spring Extension (@BeforeTransaction, @AfterTransaction)
  6. @RepeatedTest Failure Threshold • When dealing with a flaky test…

    • Have you ever wanted to abort a @RepeatedTest after the first failure or a certain number of failures? • Starting with JUnit Jupiter 5.10, you can… via the failureThreshold feature! 16
  7. Example: @RepeatedTest Failure Threshold @RepeatedTest(value = 8, failureThreshold = 2)

    void repeatedTestWithFailureThreshold(RepetitionInfo repetitionInfo) { // Simulate unexpected failure every second repetition if (repetitionInfo.getCurrentRepetition() % 2 == 0) { Assertions.fail("Boom!"); } } 17
  8. Repeatable @*Source Annotations • Since JUnit Jupiter 5.11, source annotations

    such as @ValueSource, @MethodSource, etc. can be repeated on a @ParameterizedTest method. @ParameterizedTest @MethodSource("category1Files") @MethodSource("category2Files") void processCategoryOneAndTwoFiles(Path path) { // assertions } 19
  9. @FieldSource • @FieldSource is essentially a companion to @MethodSource. •

    Allows you to source a stream of argument sets from a field. ◦ Local to your test class ◦ Or in an external class ▪ Using a fully-qualified field name ▪ For example, in a production class or a third-party utility 20
  10. Named Argument Sets • Prior to JUnit Jupiter 5.11, individual

    arguments for a @ParameterizedTest could be given a custom display name via the Named API. • With an ArgumentSet, it’s now possible to give a custom display name to an entire set of arguments. • Via the Arguments.argumentSet(String, Object...) factory method. 21
  11. Example: @FieldSource and Argument Sets @DisplayName("A parameterized test with named

    argument sets") @ParameterizedTest @FieldSource("argumentSets") void testWithArgumentSets(File file1, File file2) { } static List<Arguments> argumentSets = List.of( argumentSet("Important files", new File("path1"), new File("path2")), argumentSet("Other files", new File("path3"), new File("path4")) ); 22
  12. Arguments for @MethodSource Factory Methods • Parameterizing your parameterized tests

    😉 • Previously only possible via a custom ArgumentsProvider • @MethodSource factory methods can now accept arguments ◦ Made possible by the ExecutableInvoker ◦ Arguments provided by a ParameterResolver ◦ For example, the SpringExtension 24
  13. Demo 27 • @TempDir ◦ Cleanup mode ◦ Factory •

    @AutoClose • @Timeout ◦ Thread mode
  14. Sovereign Tech Fund • German government program to strengthen the

    open source ecosystem • Invests in foundational technologies that enable the creation of other software https://www.sovereign.tech/programs/fund Marc will work full-time on JUnit for about a year! 30
  15. JUnit 5.12 Planned release date: Feb 2025 Highlights • Open

    Test Reporting ◦ HTML test reports ◦ Attaching files such as screenshots to test results • Improved support for resource-based test engines (such as Cucumber) 31
  16. JUnit 5.13 Planned release date: ~Sep 2025 Highlights • Parameterized

    test classes • Test discovery validation mechanism • Improved usability of documentation • Better Kotlin support (contracts, Sequence support, …) • Safe cancellation of test execution 32