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

Embracing JUnit 5 with Eclipse

Noopur
October 30, 2017

Embracing JUnit 5 with Eclipse

Presentation at EclipseCon Europe, October 2017.

Noopur

October 30, 2017
Tweet

More Decks by Noopur

Other Decks in Programming

Transcript

  1. 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
  2.  JUnit 4.0 Released in 2006  JUnit 5.0 Released

    in September 2017 2 Source: 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. Eclipse support for JUnit 5 7 Eclipse Oxygen.1a (4.7.1a) https://www.eclipse.org/downloads/eclipse-packages

    Eclipse Photon (4.8) M2 onwards http://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
  8. Demo JUnit Jupiter - Programming Model 8 EclipseCon Europe 2017

    | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM
  9. Create a new JUnit Jupiter test Create a new JUnit

    Jupiter test in Eclipse with lifecycle method stubs for 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
  10. 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
  11. 11 Visibility Test classes and methods can have any access

    modifier (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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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 in or excluded from a test run via Configure Tags dialog in its JUnit launch configuration.
  19. 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
  20. 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.
  21. 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
  22. 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 which dynamically 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
  23. 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
  24. 25 Repeated Tests Repeat a test by annotating the method

    with @RepeatedTest and specifying the number of repetitions. • name attribute: to optionally specify a custom display name for each repetition. • RepetitionInfo: as test method parameter to get information about the current repetition. EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM
  25. 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
  26. 27 Test Instance Lifecycle • By default, JUnit Jupiter creates

    a new instance of the test class before executing each test method. • To execute all test methods on the same instance of the test class, you can annotate the test class with @TestInstance(Lifecycle.PER_CLASS). • The per-class mode makes it possible to use @BeforeAll and @AfterAll annotations on: o Non-static methods o Interface default methods o Methods in @Nested test classes EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM
  27. JUnit Jupiter - Extension Model See the JUnit 5 User

    Guide for more details. 28  Provides extension points as interfaces in org.junit.jupiter.api.extension package 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’s ServiceLoader mechanism. EclipseCon Europe 2017 | Embracing JUnit 5 with Eclipse | Noopur Gupta, IBM
  28. 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
  29. References & Resources Eclipse 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
  30. References & Resources JUnit 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
  31. Eclipse Converge 2017 | Embracing JUnit 5 with Eclipse |

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