Advertisement

An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito

Aug. 15, 2019
Advertisement

More Related Content

Advertisement

An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito

  1. An introduction to JUnit 5 & How to use it with Spring tests and Mockito Shaun Thomas Shaunthomas999[at]gmail.com
  2. JUnit 4 • Released 2006 (more than a decade ago) • Made use of Java 5 APIs • Fat jar – junit.jar • Supports JUnit 3
  3. JUnit 5 - Overview
  4. JUnit 5 • Released in 2017 • Works only with Java version >= 8 • JUnit 5 = Platform + Jupiter + Vintage • Supports JUnit 4 and so also JUnit 3 • Good to use with - maven-surefire-plugin >= 2.22.0
  5. Components Overview Third partyVintage Jupiter Platform Old tests New tests Third party tests Build tools/IDEs Foundation for discovering, filtering, configuring and executing tests • Launcher API – build tools & IDE • TestEngine API • ConsoleLauncher TestEngine and APIs for: • Writing Tests • New annotations & methods • Extensions • Extension API TestEngine for running JUnit 4 and JUnit 3 based tests on the platform Third party frameworks runs on platform by making use of TestEngine API
  6. Module dependencies
  7. JUnit 5 - Features
  8. Refer to project https://github.com/shaunthomas999/junit5- samples/tree/master/junit5-features
  9. Assertions • Standard assertions • Grouped assertions • Dependent assertions • Can be used with Assertj, Hamcrest etc. libs
  10. Assumptions • Purpose • Test only if certain resource is present • Test only if environment variable has certain value • Profile based testing – Env., user etc. • Contains subset of assumption methods from JUnit 4 • New method assumingThat • Throws AssumptionViolatedException to signal that test has been aborted
  11. Display name • @DisplayName • Description text • Emoji 😱
  12. Tags • @Tag • Class level / method level • Can assign multiple tags to a class / method • Filtering • IDE level • Command line • -DincludeTags • -DexcludeTags • Surefire plugin - config
  13. Create New Test Annotations • Method / Class level
  14. Repeated Tests • Execute tests specified number of times • Can get info about current repetition/iteration and total repetition by passing RepetitionInfo as method parameter • Option to customize display name for each repetition
  15. Parameterized Tests • Need additional dependency <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> • Provide values through @ValueSource, @EnumSource, @MethodSource, @CsvSource, @CsvFileSource etc.
  16. JUnit 5 with Spring Tests & Mockito
  17. Refer to project https://github.com/shaunthomas999/junit5- samples/tree/master/spring-boot-mockito-junit5
  18. Test instance Lifecycle • Test class is instantiated at per test method level by default (PER_METHOD) • Same as Junit 4 • Even for @Disabled method test class is instantiated • Ways to change it: • Class level instantiation - @TestInstance(Lifecycle.PER_CLASS) • JVM parameter – -Djunit.jupiter.testinstance.lifecycle.default=per_class • Prop. File src/test/resources > junit-platform.properties > junit.jupiter.testinstance.lifecycle.default = per_class
  19. Q & A
  20. Thanks for your attention J
Advertisement