junit_5
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| junit_5 [2020/03/11 10:37] – [Conditional Test Execution] kalaschnikow | junit_5 [2020/03/11 13:52] (current) – [Test Execution Order] kalaschnikow | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== JUnit 5 ====== | ||
| + | |||
| + | * Java 8 necessary | ||
| + | |||
| + | |||
| + | ===== Most used annotations ===== | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | * //Test Method//: | ||
| + | * any instance method that is directly annotated or meta-annotated with '' | ||
| + | * not '' | ||
| + | * not (more) required to be '' | ||
| + | |||
| + | ==== Meta-Annotations / Composed Annotations ==== | ||
| + | |||
| + | Multiple annotations can be composed to one, which can be used later instead. | ||
| + | |||
| + | === Example 1 === | ||
| + | |||
| + | Replace '' | ||
| + | |||
| + | <code java> | ||
| + | @Target({ ElementType.TYPE, | ||
| + | @Retention(RetentionPolicy.RUNTIME) | ||
| + | @Tag(" | ||
| + | public @interface Fast { | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Usage: | ||
| + | <code java> | ||
| + | @Fast | ||
| + | @Test | ||
| + | void myFastTest() { | ||
| + | // ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === Example 2 === | ||
| + | |||
| + | Combine '' | ||
| + | |||
| + | <code java> | ||
| + | |||
| + | @Target({ ElementType.TYPE, | ||
| + | @Retention(RetentionPolicy.RUNTIME) | ||
| + | @Tag(" | ||
| + | @Test | ||
| + | public @interface FastTest { | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Usage: | ||
| + | <code java> | ||
| + | @FastTest | ||
| + | void myFastTest() { | ||
| + | // ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== 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 ==== | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | ==== 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 | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Test Instance Lifecycle ==== | ||
| + | |||
| + | ==== Parameterized Tests ==== | ||
| + | |||
| + | * '' | ||
| + | |||
| + | ==== Repeated Tests ==== | ||
| + | |||
| + | * @RepeatedTest | ||
| + | |||
| + | ==== Dynamic Tests ==== | ||
| + | |||
| + | * @TestFactory | ||
| + | |||
| + | |||
| + | ==== Templates for Test Cases ==== | ||
| + | |||
| + | * @TestTemplate | ||
| + | |||
| + | |||
| + | |||
| + | === Misc === | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ==== 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 | ||
| + | |||
