Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

JUnit 5 - The Next Generation

702 views

Published on

Talk was given for the first time at JPrime 2017 conference in Sofia, Bulgaria

JUnit is the standard way to test things in Java. In fact, it is the most commonly included external library for Java projects! Here is another fact - JUnit 4 release was 11 years ago and no feature releases were added in recent years. What you knew about JUnit in 2010 is still 100% relevant today. In 2017...

So finally, time for upgrade!

What took 11 years? What is new? What changed and what stayed the same? What about IDE and build tool support? When is the release date? What will testing on the JVM look like in the future? Check the presentation and you will find out.

For more info about JPrime check out jprime.io

Published in: Software
  • Be the first to comment

JUnit 5 - The Next Generation

  1. 1. kgolev.com@kotseto JUnit 5 Kostadin Golev CTO @ Rewards Labs The Next Generation
  2. 2. kgolev.com@kotseto
  3. 3. kgolev.com@kotseto Why JUnit 5? Where Is It? Writing Tests Platform For The JVM Running Along JUnit 3 & 4 Important New Features IDE & Tool Support
  4. 4. kgolev.com@kotseto
  5. 5. kgolev.com@kotseto Issues with JUnit 4
  6. 6. kgolev.com@kotseto
  7. 7. kgolev.com@kotseto How bad? Let’s rename some private variables to find out!
  8. 8. kgolev.com@kotseto
  9. 9. kgolev.com@kotseto 4.12-beta-1: org.junit.ComparisonFailure: expected: null<null> but was: null<null> 4.11: org.junit.ComparisonFailure: expected:<[1]L> but was:<[2]L> assertEquals(1, 2)
  10. 10. kgolev.com@kotseto Extension mechanism Runner @RunWith(SpringJUnit4ClassRunner.class) Rule @Rule ExpectedException thrown = none()
  11. 11. kgolev.com@kotseto Powerful Composable Runner Rule
  12. 12. kgolev.com@kotseto Extension mechanism Powerful Composable? Runner Rule
  13. 13. kgolev.com@kotseto
  14. 14. kgolev.com@kotseto Why JUnit 5? Where Is It? Writing Tests Platform For The JVM Running Along JUnit 3 & 4 Important New Features IDE & Tool Support
  15. 15. kgolev.com@kotseto Will this work?
  16. 16. kgolev.com@kotseto There is no single JUnit 5 (JAR)
  17. 17. kgolev.com@kotseto Not one big fat jar We have more then ten now, in three groups
  18. 18. kgolev.com@kotseto JUnit Platform + JUnit Jupiter + JUnit Vintage JUnit 5
  19. 19. kgolev.com@kotseto JUnit Platform
  20. 20. kgolev.com@kotseto JUnit Jupiter JUnit Vintage
  21. 21. kgolev.com@kotseto Why JUnit 5? Where Is It? Writing Tests Platform For The JVM Running Along JUnit 3 & 4 Important New Features IDE & Tool Support
  22. 22. kgolev.com@kotseto
  23. 23. kgolev.com@kotseto
  24. 24. kgolev.com@kotseto
  25. 25. kgolev.com@kotseto
  26. 26. kgolev.com@kotseto
  27. 27. kgolev.com@kotseto @Test void exception() { RuntimeException thrown = assertThrows(RuntimeException.class, () -> library.throwRuntimeException("message") ); assertEquals("message", thrown.getMessage()); }
  28. 28. kgolev.com@kotseto
  29. 29. kgolev.com@kotseto What about Runners and Rules?
  30. 30. kgolev.com@kotseto We wrote some tests! How do we run them?
  31. 31. kgolev.com@kotseto Use JUnit 4 @RunWith(JUnitPlatform.class) provides limited JUnit 5 functionality
  32. 32. kgolev.com@kotseto Why JUnit 5? Where Is It? Writing Tests Platform For The JVM Running Along JUnit 3 & 4 Important New Features IDE & Tool Support
  33. 33. kgolev.com@kotseto Some History Or how JUnit 5 team discovered they were building a platform
  34. 34. kgolev.com@kotseto It started as a big fat jar Then everything got split in small, focused modules
  35. 35. kgolev.com@kotseto Launcher <<interface>> Engine Engine Impl Jupiter API implements JUnit Platform JUnit Jupiter
  36. 36. kgolev.com@kotseto Launcher <<interface>> Engine JUnit Platform
  37. 37. kgolev.com@kotseto <<interface>> Engine Engine Implementation implements JUnit Platform JUnit Jupiter
  38. 38. kgolev.com@kotseto Jupiter API JUnit Jupiter @Test @BeforeEach assertEquals() …
  39. 39. kgolev.com@kotseto Why JUnit 5? Where Is It? Writing Tests Platform For The JVM Running Along JUnit 3&4 Important New Features IDE & Tool Support
  40. 40. kgolev.com@kotseto You can still run your JUnit 3&4 tests But it will not be JUnit 3&4 running them
  41. 41. kgolev.com@kotseto Launcher <<interface>> Engine Vintage Engine Impl JUnit 3&4 API implements JUnit Platform JUnit Vintage
  42. 42. kgolev.com@kotseto JUnit Platform JUnit Jupiter JUnit Vintage JUnit5 tests JUnit4 tests
  43. 43. kgolev.com@kotseto Platform for the JVM Developers already started implementing their own engines, reusing JUnit5 tool integration
  44. 44. kgolev.com@kotseto Not only for Java Test Engine implementations exist for Scala, Kotlin and Groovy
  45. 45. kgolev.com@kotseto Why JUnit 5? Where Is It? Writing Tests Platform For The JVM Running Along JUnit 3 & 4 Important New Features IDE & Tool Support
  46. 46. kgolev.com@kotseto assertAll(…)
  47. 47. kgolev.com@kotseto assertEquals(1, 2); assertEquals("String", "Another String"); org.opentest4j.AssertionFailedError: Expected :1 Actual :2
  48. 48. kgolev.com@kotseto assertAll( () -> assertEquals(1, 2), () -> assertEquals("String", “Another String") ) org.opentest4j.MultipleFailuresError: Multiple Failures (2 failures) expected: <1> but was: <2> expected: <String> but was: <Another String>
  49. 49. kgolev.com@kotseto What about Runners and Rules (again)? JUnit5 Extension model
  50. 50. kgolev.com@kotseto @ExtendWith
  51. 51. kgolev.com@kotseto
  52. 52. kgolev.com@kotseto Composable & Powerful Use as many as you want whenever you want them
  53. 53. kgolev.com@kotseto @ExtendWith(SpringExtension.class) @ExtendWith(MockitoExtension.class) public class SpringTest { @Autowired SpringComponent component; @Mock Dependency mocked;
  54. 54. kgolev.com@kotseto class ParameterInTestMethods { @Test @ExtendWith(MockitoExtension.class) void test(@Mock Dependency mocked) { … }
  55. 55. kgolev.com@kotseto
  56. 56. kgolev.com@kotseto public class MockitoExtension implements TestInstancePostProcessor { @Override public void postProcessTestInstance( Object testInstance, ExtensionContext context) { MockitoAnnotations.initMocks(testInstance); } }
  57. 57. kgolev.com@kotseto Not many extensions yet a few unofficial ones Spring and Mockito support in next releases
  58. 58. kgolev.com@kotseto @Nested @DisplayName
  59. 59. kgolev.com@kotseto public class Library { private Books books; public Library(Books books) { this.books = books; } public void addBook(Book book) { // do something with books } }
  60. 60. kgolev.com@kotseto @Test void whenBookExistsThenIncrementAmount() {} @Test void whenBookExistsThenCheckAmountMoreThenN() {} @Test void whenBookDoesNotExistThenCreateBook() {} @Test void whenBookDoesNotExistThenSendNewBookNotification() {}
  61. 61. kgolev.com@kotseto
  62. 62. kgolev.com@kotseto
  63. 63. kgolev.com@kotseto
  64. 64. kgolev.com@kotseto Tests are often a tree, not a list
  65. 65. kgolev.com@kotseto
  66. 66. kgolev.com@kotseto @DisplayName("Book is added to library") class BookAddedTest { 
 @DisplayName("when book exists") @Nested class whenBookExists { @DisplayName("amount++") @Test void incrementAmount() {} @DisplayName("check amount > limit") @Test void checkAmountMoreThenLimit() {} } . . . }
  67. 67. kgolev.com@kotseto
  68. 68. kgolev.com@kotseto @ParameterizedTest
  69. 69. kgolev.com@kotseto @Test void strIsLessThenTenChar() { int value = "str".length(); assertTrue(value < 10); } @Test void StringIsLessThenTenChar() { int value = "String".length(); assertTrue(value < 10); }
  70. 70. kgolev.com@kotseto
  71. 71. kgolev.com@kotseto
  72. 72. kgolev.com@kotseto
  73. 73. kgolev.com@kotseto @ValueSource(strings = {“str”, “String”}) @ValueSource(ints = {1, 3, 7, 9}) @EnumSource(SomeEnum.class) @CsvSource({"1, 1", "2, 4", "4, 16”}) @CsvFileSource(resources=“testData.csv”)
  74. 74. kgolev.com@kotseto @MethodSource(names=“stringAndIntProvider") static Stream<Arguments> stringAndIntProvider() { return Stream.of( ObjectArrayArguments.create("foo", 3), ObjectArrayArguments.create("foobar", 6) ); }
  75. 75. kgolev.com@kotseto
  76. 76. kgolev.com@kotseto Why JUnit 5? Where Is It? Writing Tests Platform For The JVM Running Along JUnit 3 & 4 Important New Features IDE & Tool Support
  77. 77. kgolev.com@kotseto Supports latest release candidate Support in Oxygen 4.7 BETA Official support target 4.7.1
 ETA: September 2017
  78. 78. kgolev.com@kotseto Surefire provider Plugin No native support (yet)
  79. 79. kgolev.com@kotseto 6 September 2017
  80. 80. kgolev.com@kotseto junit.org/junit5
  81. 81. kgolev.com@kotseto Questions? @kotseto kgolev.com/talks/junit5

×