Advertisement
Advertisement

More Related Content

Advertisement

Recently uploaded(20)

JUnit 5 - The Next Generation

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