junit 5 alpha
TRANSCRIPT
JUnit 5 Alpha
• A refreshing update to the existing JUnit framework
• No groundbreaking new features or changes, but finally taking advantage of Java 8
• No conflict with existing JUnit 4 tests
• The JUnit 5 API has been placed under the org.junit.gen5 package
• JUnit 5 tests may be run along side JUnit 4 tests, in your IDE
• Use @RunWith(JUnit5.class) and your IDE should pick them up just like any other JUnit tests
• Test classes and methods no longer have to be public, but can be package private
Familiar annotations• @Test
• @Before
• @After
• @BeforeAll
• @AfterAll
Name your tests
• @DisplayName(“More readable name”)
Disabled tests
• @Disabled instead of @Ignore
Assertions
• assertEquals now takes optional assertion message as last parameter
• assertAll enables grouped assertions
• Better exception testing via expectThrows and assertThrows assertion methods
Assumptions• assumeTrue
• assumingThat
Tags
• Similar to categories, but extended beyond @Category
• @Tag(“web-tests”)
Nested tests
• Nest test classes for better grouping and organisation
• Note that @BeforeAll and @AfterAll does not work on the nested classes
Constructor and method parameters
• JUnit 5 allows constructors and methods to have parameters
• ParameterResolver defines an API for resolving these parameters at runtime and provide them to the tests
Composed annotations• JUnit 5 annotations can be used as meta-
annotations, meaning you can compose your own annotations based on these annotations
Extension model• Good bye Runner, Rule and ClassRule
• Hello, @ExtendWith!
• ContainerExecutionCondition and TestExecutionCondition evaluates whether or not to execute a test class, or single test
Let’s see this in action!
Thank you!twitter.com/jaranflaath [email protected]
Image CC BY-SA 4.0 Ashashyou (https://commons.wikimedia.org/wiki/User:Ashashyou)