Slide 1

Slide 1 text

5 JUNIT 5 MORE THAN JUST A TESTING FRAMEWORK FOR JAVA

Slide 2

Slide 2 text

5 MARC PHILIPP So ware Engineer at JUnit Maintainer since 2012 Twi er: Web: @marcphilipp marcphilipp.de

Slide 3

Slide 3 text

5 ✋ SHOW OF HANDS

Slide 4

Slide 4 text

5 JUNIT 5 IS HERE! 5.0 September 10, 2017 5.1 February 18, 2018 5.2 April 29, 2018 5.3 September 11, 2018

Slide 5

Slide 5 text

5 AGENDA 1. How to write tests and extensions using JUnit 5? 2. What is the JUnit Pla orm and why do we need it? 3. What’s s ll to come and how to get started?

Slide 6

Slide 6 text

5 JUNIT JUPITER JUNIT JUPITER JUNIT JUPITER JUNIT JUPITER JUNIT JUPITER THE NEW TESTING FRAMEWORK FOR JAVA THE NEW TESTING FRAMEWORK FOR JAVA THE NEW TESTING FRAMEWORK FOR JAVA THE NEW TESTING FRAMEWORK FOR JAVA THE NEW TESTING FRAMEWORK FOR JAVA Image: NASA

Slide 7

Slide 7 text

5 JUPITER? Nope, it’s just a new name so we can easily dis nguish it from the old JUnit and the other parts of JUnit 5. … and it’s the fi h planet from the sun. Is wri ng tests rocket science now?

Slide 8

Slide 8 text

5 BASICS (DEMO) h ps:/ /github.com/marcphilipp/junit5‑ demo/tree/20181107‑wjax

Slide 9

Slide 9 text

5 BASICS (RECAP) @Test is s ll a thing, but in org.junit.jupiter.api @Disabled instead of @Ignore @BeforeAll , @BeforeEach , @AfterEach , @AfterAll have new names Assertions look similar – a few new ones like assertThrows , assertAll Custom @DisplayNames to ease camel‑case fa gue @TestInstance(PER_METHOD or PER_CLASS) @Tag instead of @Category

Slide 10

Slide 10 text

5 MORE WAYS TO TEST (DEMO) h ps:/ /github.com/marcphilipp/junit5‑ demo/tree/20181107‑wjax

Slide 11

Slide 11 text

5 MORE WAYS TO TEST (RECAP) @ParameterizedTest with different @Source annota ons @ValueSource , @EnumSource , @CsvSource , @CsvFileSource , @MethodSource , @ArgumentsSource(MyProvider.class) , @YourCustomSource @RepeatedTest for flaky tests @TestFactory to produce dynamic tests

Slide 12

Slide 12 text

5 PARALLEL EXECUTION (DEMO) h ps:/ /github.com/marcphilipp/junit5‑ demo/tree/20181107‑wjax

Slide 13

Slide 13 text

5 PARALLEL EXECUTION (RECAP) Tests are run sequen ally by default Opt‑in and configure parallel execu on via configura on parameters @Execution(SAME_THREAD or CONCURRENT) Use @ResourceLock as declara ve synchroniza on mechanism

Slide 14

Slide 14 text

5 EXTENSIONS (DEMO) h ps:/ /github.com/marcphilipp/junit5‑ demo/tree/20181107‑wjax

Slide 15

Slide 15 text

5 EXTENSIONS (RECAP) Registra on (as many as you need): Declara ve: @ExtendWith on classes or methods Programma c: @RegisterExtension on fields Global: Via ServiceLoader (see ) Implementa on: Extension marker interface one extension – n extension points/interfaces User Guide

Slide 16

Slide 16 text

5 COMPOSED ANNOTATIONS You can use Jupiter annota ons as meta‑annota ons to create your own annota ons. @Retention(RUNTIME) @Target(METHOD) @ExtendWith(ConferenceExecutionCondition.class) @Tag("example") public @interface DisabledOnConference {}

Slide 17

Slide 17 text

5 EXTENSION POINTS Lifecycle: BeforeAllCallback , BeforeEachCallback , BeforeTestExecutionCallback , TestExecutionExceptionHandler , AfterTestExecutionCallback , AfterEachCallback , AfterAllCallback Other: ExecutionCondition , TestInstanceFactory , TestInstancePostProcessor , ParameterResolver , TestTemplateInvocationContextProvider

Slide 18

Slide 18 text

5 THIRD‑PARTY EXTENSIONS JUnit Pioneer, Spring, Mockito, Testcontainers, Docker, Wiremock, JPA, Selenium/WebDriver, DbUnit, Ka a, Jersey, GreenMail, S3Mock, Citrus Framework, XWiki, … h ps:/ /github.com/junit‑team/junit5/wiki/Third‑party‑ Extensions

Slide 19

Slide 19 text

5 AGENDA 1. How to write tests and extensions using JUnit 5? ✅ 2. What is the JUnit Pla orm and why do we need it? 3. What’s s ll to come and how to get started?

Slide 20

Slide 20 text

5 JUNIT PLATFORM JUNIT PLATFORM JUNIT PLATFORM JUNIT PLATFORM JUNIT PLATFORM PLATFORM FOR TESTING ON THE JVM PLATFORM FOR TESTING ON THE JVM PLATFORM FOR TESTING ON THE JVM PLATFORM FOR TESTING ON THE JVM PLATFORM FOR TESTING ON THE JVM Image: NASA

Slide 21

Slide 21 text

5 JUNIT AS A PLATFORM? JUnit has always been a pla orm for IDEs and build tools for other tes ng frameworks Closely coupled (internal APIs, reflec on, serializa on)

Slide 22

Slide 22 text

5 COUPLING

Slide 23

Slide 23 text

5 If JUnit is a pla orm, let’s design for it!

Slide 24

Slide 24 text

5 SEPARATION OF CONCERNS 1. An API to write tests and extensions (Jupiter API) 2. Extensible mechanisms to discover and execute tests (Test Engine SPI) 3. An API for test execu on by tools (Launcher API)

Slide 25

Slide 25 text

5 DESIGN GOALS Flexibility: Adding new features should be easy. It should be clear whether a change might poten ally break a client. Backward Compa bility: Test wri en with JUnit 3 and 4 should s ll run Forward Compa bility: Old IDEs and build tools should be able to execute new tests

Slide 26

Slide 26 text

5

Slide 27

Slide 27 text

5 JUNIT 5 = JUPITER + VINTAGE + PLATFORM

Slide 28

Slide 28 text

5 THIRD‑PARTY ENGINES Specsy, Spek, KotlinTest, Cucumber, Drools, jqwik, Brahms, … h ps:/ /github.com/junit‑team/junit5/wiki/Third‑party‑ Extensions

Slide 29

Slide 29 text

5 COMPATIBILITY / MIGRATION Vintage Engine to run JUnit 3/4 tests on the Pla orm @Category(UI.class) maps to com.acme.UI tag Limited support for JUnit 4 Rules to ease migra on Migra on support for @Ignore (will be in 5.4) IDEs provide tools to convert test classes to Jupiter API Community‑provided migra on tool: h ps:/ /github.com/boyarsky/convert‑junit4‑to‑junit5

Slide 30

Slide 30 text

5 BUILD TOOL SUPPORT Na ve support in Gradle (≥ 4.6), Ant (≥ 1.10.3), and Maven Surefire (≥ 2.22.0) ConsoleLauncher to run tests from the command line or to support other build tools (e.g. Bazel)

Slide 31

Slide 31 text

5 IDE SUPPORT Excellent support IntelliJ IDEA (≥ 2016.2) Eclipse (≥ 4.7.1a) Visual Studio Code (Java Test Runner ≥ 0.4.0) Netbeans (will come with 10.0) For other tools, there’s @RunWith(JUnitPlatform)

Slide 32

Slide 32 text

5 USING MULTIPLE ENGINES (DEMO) h ps:/ /github.com/marcphilipp/junit5‑pla orm‑demo

Slide 33

Slide 33 text

5 USING MULTIPLE ENGINES (RECAP) Mul ple test engines can be used in a single test run Dis nc on between testImplementation and testRuntimeOnly dependencies Allows to gradually migrate tests from one test engine to another (e.g. from Vintage to Jupiter)

Slide 34

Slide 34 text

5 AGENDA 1. How to write tests and extensions using JUnit 5? ✅ 2. What is the JUnit Pla orm and why do we need it? ✅ 3. What’s s ll to come and how to get started?

Slide 35

Slide 35 text

5 ROADMAP AND ROADMAP AND ROADMAP AND ROADMAP AND ROADMAP AND RESOURCES RESOURCES RESOURCES RESOURCES RESOURCES Image: NASA

Slide 36

Slide 36 text

5 IMPORTANT FUTURE MILESTONES Test ordering (e.g. randomiza on) ✅ will be in 5.4 New repor ng format that supports new features Declara ve Test Suites Parameterized Test Classes Scenario Tests Your ideas?

Slide 37

Slide 37 text

5 GETTING STARTED User Guide: Sample projects for Ant, Bazel, Gradle, and Maven: Javadoc: h p:/ /junit.org/junit5/docs/current/user‑guide/ h ps:/ /github.com/junit‑team/junit5‑samples h p:/ /junit.org/junit5/docs/current/api/

Slide 38

Slide 38 text

5 WANTED: FEEDBACK! StackOverflow: Code & Issues: Gi er: Twi er: h p:/ /stackoverflow.com/ques ons/tagged/junit5 h ps:/ /github.com/junit‑team/junit5/ h ps:/ /gi er.im/junit‑team/junit5 h ps:/ /twi er.com/juni eam

Slide 39

Slide 39 text

5 DONATIONS Support the JUnit team with dona ons via Steady: h ps:/ /steadyhq.com/en/junit

Slide 40

Slide 40 text

5 EXAMPLE CODE Jupiter: Pla orm: h ps:/ /github.com/marcphilipp/junit5‑ demo/tree/20181107‑wjax h ps:/ /github.com/marcphilipp/junit5‑pla orm‑demo

Slide 41

Slide 41 text

5 QUESTIONS? or on Twi er @marcphilipp @juni eam

Slide 42

Slide 42 text

5 THANKS!