What is new in JUnit5

15
JUnit5 The Next Generation of JUnit Richard Langlois P. Eng. Solutions Architect 10 août 2016

Transcript of What is new in JUnit5

Page 1: What is new in JUnit5

JUnit5 – The Next Generation of JUnit Richard Langlois P. Eng. Solutions Architect 10 août 2016

Page 2: What is new in JUnit5

Why JUnit5

What is New

Demo

Migration Tips

10 août 2016

Agenda

Page 3: What is new in JUnit5

Junit 4 is an All-in-one library

Difficult for IDEs to deal with the library.

10 août 2016

Why Junit 5

Page 4: What is new in JUnit5

Revamped codebase with a modular architecture.

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

Junit Platform: Defines TestEngine API for developing a testing framework that runs on the platform.

Junit Jupiter: provides TestEngine for running Junit 5 based tests on the platform.

Junit Vintage: provides TestEngine for running Junit 3 and Junit 4 based tests on the platform.

Test code depends only on the JUnit 5 API.

IDE support: As of August 2016, supported by IntelliJ IDEA 2016.2

Milestone 2 released on July 23, 2016

GA release (due late 2016).

Code samples available on GitHub (https://github.com/junit-team/junit5-samples)

10 août 2016

What is Junit 5

Page 5: What is new in JUnit5

MODIFIEZ LE STYLE DU TITRE

Architecture

• Test code depends only on the JUnit 5 API.

• IDEs and build tools depend on the Launcher and Engine APIs and can execute tests independent of the testing framework in use.

10 août 2016

Page 6: What is new in JUnit5

JUnit 4

Import org.junit.Test

public class JUnit4Test {

@Test

public void aJunit4TestCase() {

assertTrue(“message”, 5 > 6);

}

}

JUnit 5

Import org.junit.jupiter.api.Test

class JUnit5Test {

@Test

void aJunit5TestCase() {

assertTrue(5 > 6, “message”);

}

}

10 août 2016

JUnit 4 vs JUnit 5

Page 7: What is new in JUnit5

New set of annotations which resides in a different package (org.junit.jupiter.api) than their JUnit 4 counterparts:

@Test: the method is a test

@TestFactory: the method is a test factory for dynamic tests

@DisplayName: declares a custom display name (class or method)

@BeforeEach: method executed before each test method

@AfterEach: method executed after each test method

@BeforeAll: method executed before all test methods

@AfterAll: method executed after all test methods

@Nested: denotes that the class is a nested, non-static test class

@Tag: declare a tag for filtering tests (class or method)

@Disabled: disable a test class or test method

@ExtendWith: register custom extensions

10 août 2016

What is New

Page 8: What is new in JUnit5

New package org.junit.jupiter.api

Tests do not need to be public

Meta-Annotation: Allows to define our own composed annotation

Display Names with @DisplayName(“…”)

Can contain spaces, special chars, and emoji 😱

Assertions

Optional message is now the last parameter

Grouped assertions using assertAll()

expectThrows: check that method throws an exception, and returns the exception object to allow further testing.

10 août 2016

What is New (continued)

Page 9: What is new in JUnit5

Assumptions: Skip the test execution if the assumption condition is not met.

E.g. assumeTrue, assumingThat

Disabling test with @Disabled

Tagging: used to enable/disable tests with specific tags in a build and execution

Equivalent of the Categories in JUnit 4

e.g. @Tag(“fast”).

Nested Tests: Allow expressing relationship among several group of tests.

10 août 2016

What is New (continued)

Page 10: What is new in JUnit5

Extension Model:

New extension API that supersedes the previous @RunWith and @Rule extension mechanism

Test constructors and methods are now permitted to have parameters enabling dependency injection.

2 built-in resolvers: TestInfo: information about the current test (e.g. display name)

TestReporter: to publish additional data about the current test run

Custom ParameterResolver: Specified with @ExtendWith

10 août 2016

What is New (continued)

Page 11: What is new in JUnit5

Dynamic tests:

A DynamicTest is a test case generated at runtime.

Generated at runtime by a factory method (annotated with @TestFactory).

@TestFactory method returns a Stream, Collection, Iterable or Iterator of DynamicTest instances.

10 août 2016

What is New (continued)

Page 12: What is new in JUnit5

1. AnnotationTestDemo.java

2. MetaAnnotationDemo.java

3. BeforeAfterDemo.java

4. DisplayNameDemo.java

5. AssertionsDemo.java

6. AssumptionsDemo.java

7. DisabledClassDemo.java

8. DisabledTestDemo.java

9. TaggingDemo.java

10. NestedTestDemo.java

11. ParameterTestInfoDemo.java

12. ParameterTestReporterDemo.java

13. DynamicTestDemo.java

10 août 2016

Demo

Page 13: What is new in JUnit5

Annotations reside in the org.junit.jupiter.api package.

Assertions reside in org.junit.jupiter.api.Assertions.

Assumptions reside in org.junit.jupiter.api.Assumptions.

@Before and @After no longer exist; use @BeforeEach and @AfterEach instead.

@BeforeClass and @AfterClass no longer exist; use @BeforeAll and @AfterAll instead.

@Ignore no longer exists: use @Disabled instead.

@Category no longer exists; use @Tag instead.

@RunWith no longer exists; superseded by @ExtendWith.

@Rule and @ClassRule no longer exist; superseded by @ExtendWith.

10 août 2016

JUnit 4 to JUnit 5 Migration Tips

Page 15: What is new in JUnit5

Questions?

Next Lunch & Learn:

Introduction to Docker, by John Okrasa

10 août 2016

Thank You !