User Tools

Site Tools


junit_5

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
junit_5 [2020/03/11 10:26] – [Parameterized Tests] kalaschnikowjunit_5 [2020/03/11 13:52] (current) – [Test Execution Order] kalaschnikow
Line 1: Line 1:
 +====== JUnit 5 ======
 +
 +  * Java 8 necessary
 +
 +
 +===== Most used annotations =====
 +  * ''@Test''
 +  * ''@BeforeEach''
 +  * ''@BeforeAll''
 +  * ''@AfterEach''
 +  * ''@AfterAll''
 +  * ''@Disabled''
 +
 +  * //Test Method//: 
 +    * any instance method that is directly annotated or meta-annotated with ''@Test'', ''@RepeatedTest'', ''@ParameterizedTest'', ''@TestFactory'', or ''@TestTemplate''.
 +    * not ''private''
 +    * not (more) required to be ''public''
 +
 +==== Meta-Annotations / Composed Annotations ====
 +
 +Multiple annotations can be composed to one, which can be used later instead.
 +
 +=== Example 1 ===
 +
 +Replace ''@Tag("fast")'' with ''@Fast''
 +
 +<code java>
 +@Target({ ElementType.TYPE, ElementType.METHOD })
 +@Retention(RetentionPolicy.RUNTIME)
 +@Tag("fast")
 +public @interface Fast {
 +}
 +</code>
 +
 +Usage:
 +<code java>
 +@Fast
 +@Test
 +void myFastTest() {
 +    // ...
 +}
 +</code>
 +
 +=== Example 2 ===
 +
 +Combine ''@Tag("fast")'' and ''@Test'' to one ''@FastTest''.
 +
 +<code java>
 +
 +@Target({ ElementType.TYPE, ElementType.METHOD })
 +@Retention(RetentionPolicy.RUNTIME)
 +@Tag("fast")
 +@Test
 +public @interface FastTest {
 +}
 +</code>
 +
 +Usage:
 +<code java>
 +@FastTest
 +void myFastTest() {
 +    // ...
 +}
 +</code>
 +
 +==== Assertions ====
 +
 +  * Using lambdas
 +  * Grouped assertions
 +  * Dependent assertions
 +  * Exception testing
 +  * Timeout not exceeded
 +    * (simply)
 +    * with result
 +    * with method
 +  * Timeout exceeded
 +    * (simply)
 +    * with preemptive termination
 +
 +
 +  * Kotlin assertions support
 +  * Third party library support
 +    * AssertJ
 +    * Hamcrest
 +    * Truth
 +
 +==== Assumptions ====
 +
 +In direct contrast to failed assertions, failed assumptions do not result in a test failure; rather, a failed assumption results in a test being aborted.
 +
 +Assumptions are typically used whenever it does not make sense to continue execution of a given test method — for example, if the test depends on something that does not exist in the current runtime environment.
 +
 +  * assumeTrue()
 +  * assumeFalse()
 +  * assumingThat()
 +
 +==== Conditional Test Execution ====
 +
 +  * ''@DisabledOnOs''
 +  * ''@DisabledOnJre'' / ''@DisabledForJreRange''
 +  * ''@DisabledIfEnvironmentVariable'' / ''@DisabledIfEnvironmentVariables''
 +  * ''@DisabledIfSystemProperty'' / ''@DisabledIfSystemProperties''
 +  * ''@EnabledOnOs''
 +  * ''@EnabledOnJre'' / ''@EnabledForJreRange''
 +  * ''@EnabledIfEnvironmentVariable'' / ''@EnabledIfEnvironmentVariables''
 +  * ''@EnabledIfSystemProperty'' / ''@EnabledIfSystemProperties''
 +==== Test Execution Order ====
 +
 +<code java>
 +@TestMethodOrder(OrderAnnotation.class)
 +class OrderedTestsDemo {
 +
 +    @Test
 +    @Order(1)
 +    void nullValues() {
 +        // perform assertions against null values
 +    }
 +
 +    @Test
 +    @Order(2)
 +    void emptyValues() {
 +        // perform assertions against empty values
 +    }
 +
 +    @Test
 +    @Order(3)
 +    void validValues() {
 +        // perform assertions against valid values
 +    }
 +
 +}
 +</code>
 +
 +==== Test Instance Lifecycle ====
 +
 +==== Parameterized Tests ====
 +
 +  * ''@ParameterizedTest''
 +
 +==== Repeated Tests ====
 +
 +  * @RepeatedTest
 +
 +==== Dynamic Tests ====
 +
 +  * @TestFactory
 +
 +
 +==== Templates for Test Cases ====
 +
 +  * @TestTemplate
 +
 +
 +
 +=== Misc ===
 +
 +  * ''@TestMethodOrder'' - defines test method execution order
 +  * ''@TestInstance'' - ???
 +  * ''@DisplayName'' - declares custom display name for test
 +  * ''@DisplayNameGeneration'' - declares custom display name generator
 +  * ''@Nested'' - ???
 +  * ''@Tag'' - makes possible grouping of tests either on class or on method level
 +  * ''@Timeout'' - used to fail tests, if their execution exceeds a given duration
 +  * ''@ExtendWith'' - ???
 +  * ''@RegisterExtension'' - ???
 +  * ''@TempDir'' - ???
 +
 +
 +----
 +
 +==== JUnit 5 Pros ====
 +  * More granularity
 +  * Multiple runners work simultaneously
 +  * Java 8 language features
 +
 +==== JUnit 4 -> JUnit 5 ====
 +
 +  * @Before -> @BeforeEach
 +  * @After -> @AfterEach
 +  * @BeforeClass -> @BeforeAll
 +  * @AfterClass -> @AfterAll
 +  * @Ignore -> @Disabled
 +