From 58057b182bd9dcf2d869a30faa2611e0dcc78a28 Mon Sep 17 00:00:00 2001 From: Jonathan Christison Date: Tue, 25 Aug 2020 00:09:26 +0100 Subject: [PATCH] Added lots of JUnit examples --- README.md | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cb14452..32140e8 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ uni.onItem() * Microprofile + SmallRye * JUnit -* TestNG +* TestNG - Many of its features on the roadmap for JUnit 5.x anyway * Jenkins, CI, piplines Blue Ocean * Maven - Already know most of this * Have fixed bugs in maven plugins @@ -94,7 +94,75 @@ uni.onItem() * Vintage - API for older JUnit4 tests * Ext - 3rd Party extensions (custom API aka SPI) +"No news is good news" - We only care for failures +* `@Test` annotation to highlight test method we'd like to run as part of JUnit tests +* Common assert methods + +```java +assertEquals(expected, actual, "Description of failure") // Asserts both values are equal +assertArrayEquals(expectedArray, actualArray) //Verifies each item in the array are equal in the right position/order +assertIterableEquals(expectedArray, actualArray) // Verifies iterables, again equal and in the right position/order +fail() //Always fail... duh +assertAll( + () -> assertEquals(expected, actual), + //() -> ... + ) +``` +Surefire plugin in maven to run JUnit tests (thing we always disabled in Prod :wink:) + +* Test lifecycle annotations +```java +@BeforeAll //needs to be static method +@AfterAll //needs to be a static method +@BeforeEach +@AfterEach + +//eg init a new pojo for each test +class Foo { + SomePojo pojo; + @BeforeEach + void init() { + pojo = new SomePojo(); + } +} +``` + +* Nested + +```java + +class Foo { + + @Nested + class Bar { + @Test + @DisplayName("Test Description") + void dosomething() + { + assertEquals(expected, pojo.method(x,y), "Test description"); + assertEquals(Somethinexpected, pojo.method(x), "Test description 2" + } + void dosomethingelse() + { + assertEquals(expected, pojo.method(x,y), "Test description"); + assertEquals(Somethinexpected, pojo.method(x), "Test description 2" + } + } +} +``` + +* Lambda for lazy tests + +```java +assertEquals(expected, actual, () -> "big bad string: " + expected + "actual: " + actual) //Will only execute lambda if assert fires +``` + +* Repeated test `@RepeatedTest(n)` - Repeat a test multiple times (n number of times) has [RepetitionInfo](https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/RepetitionInfo.html) + +* `@Tag` annotation allows running of many or few of multiple tags + +* JUnit provides many providers (injected) eg. `RepetitionInfo` or `testInfo` ### QE tests