34
Noopur Gupta Eclipse JDT co-lead IBM India [email protected] Embracing with 1 @noopur2507 EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

  • Upload
    others

  • View
    61

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

Noopur Gupta

Eclipse JDT co-lead

IBM India

[email protected]

Embracing

with

1

@noopur2507

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 2: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

JUnit 4.0 Released in 2006

JUnit 5.0 Released in September 2017

2Source: http://blog.takipi.com/the-top-100-java-libraries-in-2017-based-on-259885-source-files/

JUnit Framework

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

The top 20 Java libraries on GitHub in 2017

Page 3: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

Modularity (single JAR)

Developers accessing internals and duplicating code

Maintainability

Extensibility(via Runners and Rules)

Java 8 support(Lambdas, Streams, Default Methods etc.)

JUnit 4 Architecture

3

junit.jar(Single JUnit 4 JAR)

Testing frameworks developers

Build tools developers

IDEs developers

@Test developers

Problems

(+ org.hamcrest.core.jar)

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 4: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

4

JUnit 5 Architecture

JUnit 5 = Platform + Jupiter + Vintage

5th planet in Solar System!

Source: http://junit.org/junit5/docs/current/user-guide/#dependency-diagram

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 5: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

5

JUnit Platform ▪ junit-platform-launcher

Launcher APIs used by IDEs and build tools to launch the framework.Finds test engines via Java’s ServiceLoader mechanism.

▪ junit-platform-engine

TestEngine APIs for integration of any testing framework that runs on platform.

JUnit Jupiter ▪ junit-jupiter-api

APIs for the new programming and extension model.▪ junit-jupiter-engine

JupiterTestEngine - To discover and execute Jupiter tests.

JUnit Vintage▪ junit-vintage-engine

To discover and execute JUnit 3 and JUnit 4 tests on JUnit 5 platform.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

JUnit 5 Architecture

Page 6: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

A sneak peek into the major interesting features of JUnit Jupiter with Eclipse support for JUnit 5.

6

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 7: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

Eclipse support for JUnit 5

7

Eclipse Oxygen.1a (4.7.1a)https://www.eclipse.org/downloads/eclipse-packages

Eclipse Photon (4.8) M2 onwardshttp://download.eclipse.org/eclipse/downloads

Software site repository to update Eclipse Oxygen (4.7 and 4.7.1):http://download.eclipse.org/releases/oxygen/

Demo: Eclipse Photon (4.8) M2 milestone build

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 8: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

Demo

JUnit Jupiter - Programming Model

8

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 9: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

Create a new JUnit Jupiter test

Create a new JUnit Jupiter test inEclipse with lifecycle method stubsfor a class and its methods under test:

9

New JUnit Test Case wizard-> New JUnit Jupiter test

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 10: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

Add JUnit 5 library to the build path

10

New JUnit Test Case wizard offers to add it while creating a new JUnit Jupiter test.

Quick Fix (Ctrl+1) proposal on @Test.

Add library (JUnit) in Java Build Path dialog.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 11: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

11

Visibility

Test classes and methods can have any accessmodifier (other than private).

Annotations

JUnit 4 JUnit Jupiter

@org.junit.Test @org.junit.jupiter.api.Test(No expected and timeout attributes)

@BeforeClass @BeforeAll

@AfterClass @AfterAll

@Before @BeforeEach

@After @AfterEach

@Ignore @Disabled

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 12: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

Create a JUnit Jupiter test method in Eclipse with the new template:

12

test_jupiter

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Create a new JUnit Jupiter test method

Page 13: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

13

Assertions(org.junit.jupiter.api.Assertions class)

• Failure message comes at the end of arguments list.

• Failure message can be retrieved lazily.

• Grouped assertions to execute all assertions first and then report all failures together.

• Exception testing to assert and evaluate a thrown exception.

• Asserting that the given task completes before the given timeout.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 14: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

14

(org.junit.jupiter.api.Assumptions class)

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Assumptions

Page 15: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

JUnit Jupiter’s Assertions, Assumptions, DynamicContainer & DynamicTest classes are now added to Eclipse Favorites by default.

15

Preferences

-> Java -> Editor

-> Content Assist

-> Favorites

Import static methods in your code from favorite classes via Content Assist (Ctrl + Space) and Quick Fix (Ctrl + 1).

Configure the number of static member imports needed before type.* is used.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Eclipse Favorites

Page 16: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

16

@DisplayName

Provide custom display names for test classes and test methods - with spaces, special characters, and even emojis!

Use Go to File action or just double-click to navigate to the test from JUnit view.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 17: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

17

@Nested test classes Non-static nested classes (i.e. inner classes) can serve as @Nested tests for logical grouping of test cases.

(Re-)Run a single @Nested test class by using the Run action in JUnit view or Outline view.You can even right-click on a nested test class name in the editor and use the Run As action.

Example: TestingAStackDemo in JUnit 5 user guide.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 18: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

18

Test interfaces and default methods

• Default test methods can be inherited by implementing test classes.

• Enables multiple inheritance in tests classes.

Example: StringTests in JUnit 5 user guide.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 19: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

19

Tagging and Filtering

• Tag test classes and test methods with @Tag.

• Tags can later be used to filter test execution.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Provide tags to be included inor excluded from a test runvia Configure Tags dialog in itsJUnit launch configuration.

Page 20: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

20

Meta-annotations and composed annotations

• JUnit Jupiter annotations can be used as meta-annotations.

• Create custom composed annotation inheriting semantics of its meta-annotations.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 21: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

21

Dynamic Tests• Dynamic tests are generated at runtime by a @TestFactory method.

Create a @TestFactory method in Eclipse with the new template:

• Dynamic container is composed of a display name and a list of dynamic nodes.

• Dynamic test is composed of a display name and an Executable.

test_factory

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

There are no lifecycle callbacks for individual dynamic tests.

Page 22: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

22

Navigate to failing source location

Double-click an entry in JUnit view’s failure trace to jump to the corresponding source location.

Click "Show Stack Trace in Console View" button and use the hyperlinks.It can also be used to copy parts of the stack trace.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 23: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

23

Dependency Injection• Test constructors and methods are now permitted to have parameters enabling

Dependency Injection.

• ParameterResolver is the extension API to provide a parameter resolver whichdynamically resolves a parameter at runtime.

• A parameter resolver can be registered via @ExtendWith(…) annotation.

• JUnit Jupiter provides some built-in resolvers which are registered automatically:

o TestInfo (resolved by TestInfoParameterResolver) to access information about the currently executing test:

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 24: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

24

Dependency Injection

o TestReporter (resolved by TestReporterParameterResolver) to publish additional data from the currently executing test which can be seen in the Console view in Eclipse:

• To support overloaded test methods, the Test Method Selection dialog in JUnit launch configuration now shows the method parameter types also:

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 25: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

25

Repeated Tests

Repeat a test by annotating the method with @RepeatedTest and specifying thenumber of repetitions.

• name attribute: to optionally specify a custom display name for each repetition.

• RepetitionInfo: as test method parameter to get information about the currentrepetition.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 26: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

26

Parameterized Tests

Run a test multiple times with different arguments by:

• annotating the method with @ParameterizedTest• and declaring at least one source to provide the arguments.

• name attribute: to optionally specify a custom display name for each invocation.• Test method may have additional parameters to be resolved by other

ParameterResolvers at the end of the method's parameter list.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 27: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

27

Test Instance Lifecycle

• By default, JUnit Jupiter creates a new instance of the test class beforeexecuting each test method.

• To execute all test methods on the same instance of the test class, you canannotate the test class with @TestInstance(Lifecycle.PER_CLASS).

• The per-class mode makes it possible to use @BeforeAll and @AfterAllannotations on:

oNon-static methods

o Interface default methods

oMethods in @Nested test classes

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 28: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

JUnit Jupiter - Extension Model

See the JUnit 5 User Guide for more details.

28

Provides extension points as interfaces in org.junit.jupiter.api.extensionpackage to be implemented by extension providers.

Register one or more extensions declaratively on a test class, test method,or composed annotation with @ExtendWith(…).

JUnit Jupiter also supports global extension registration via Java’sServiceLoader mechanism.

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 29: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

In summary…

29

Modular architecture

Modern programming model

Powerful extension model

Gentle migration path

Eclipse IDE support

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 30: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

References & ResourcesEclipse support for JUnit 5

https://www.eclipse.org/community/eclipse_newsletter/2017/october/article5.php

Download Photon (4.8) M2 & above: http://download.eclipse.org/eclipse/downloads

Download Oxygen.1a (4.7.1a): https://www.eclipse.org/downloads/eclipse-packages

Software site to update Oxygen (4.7 & 4.7.1): http://download.eclipse.org/releases/oxygen/

Report bugs/enhancements: https://bugs.eclipse.org/bugs/enter_bug.cgi?product=JDT

30

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 31: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

References & ResourcesJUnit 5 Project

http://junit.org/junit5

User Guide http://junit.org/junit5/docs/current/user-guide

Javadoc http://junit.org/junit5/docs/current/api

GitHub https://github.com/junit-team/junit5

Gitter https://gitter.im/junit-team/junit5

Q&A http://stackoverflow.com/questions/tagged/junit5

31

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 32: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

32

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 33: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

33

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

Page 34: Embracing with Eclipse JUnit 5 with...5 JUnit Platform junit-platform-launcher Launcher APIs used by IDEs and build tools to launch the framework. Finds test engines via Java’s ServiceLoader

Eclipse Converge 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM

34

EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM