Slide 1

Slide 1 text

The JUnit Crew Presents What’s New IntelliJ IDEA Livestream, Nov 2024

Slide 2

Slide 2 text

The JUnit crew Christian Marc Juliette Sam 2 Duke

Slide 3

Slide 3 text

Intro 3 Marc

Slide 4

Slide 4 text

Maven Central Downloads (JUnit Jupiter) 4

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

JUnit Jupiter is the most used testing framework for Java! 🎉🎉🎉 6

Slide 7

Slide 7 text

Thank you to our sponsors! https://junit.org/sponsoring 7

Slide 8

Slide 8 text

Platform & Console 8 Christian

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

@Suite Lifecycle Methods Extension API Improvements 11 Juliette

Slide 12

Slide 12 text

@Suite Lifecycle Methods ● New annotations in junit-platform-suite-api ○ @BeforeSuite ○ @AfterSuite 12

Slide 13

Slide 13 text

Test Instance Pre-construct Callback 13 ● Extension invoked prior to test instances being constructed ● Symmetric call to TestInstancePreDestroyCallback

Slide 14

Slide 14 text

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)

Slide 15

Slide 15 text

Parameterized & Repeated Tests 15 Sam

Slide 16

Slide 16 text

@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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Example: @RepeatedTest Failure Threshold 18

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

@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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Example: @FieldSource and Argument Sets @DisplayName("A parameterized test with named argument sets") @ParameterizedTest @FieldSource("argumentSets") void testWithArgumentSets(File file1, File file2) { } static List argumentSets = List.of( argumentSet("Important files", new File("path1"), new File("path2")), argumentSet("Other files", new File("path3"), new File("path4")) ); 22

Slide 23

Slide 23 text

Example: @FieldSource and Argument Sets 23

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Demo 25 @MethodSource and Spring

Slide 26

Slide 26 text

Core Extension Improvements 26 Marc

Slide 27

Slide 27 text

Demo 27 ● @TempDir ○ Cleanup mode ○ Factory ● @AutoClose ● @Timeout ○ Thread mode

Slide 28

Slide 28 text

Outlook 28 Marc

Slide 29

Slide 29 text

The Sovereign Tech Fund is investing in JUnit in 2024 and 2025! 🎉🎉🎉 29

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Resources ● Today’s code examples: https://github.com/junit-team/junit5-demos-nov-2024 ● User Guide, Javadoc, and everything else: junit.org 33

Slide 34

Slide 34 text

Q & A More questions? ● StackOverflow: http://stackoverflow.com/questions/tagged/junit5 ● GitHub: https://github.com/junit-team/junit5/discussions 34