Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Migrating to JUnit 5

Migrating to JUnit 5

by Joanna Hu at TWJUG 20201021 https://twjug.kktix.cc/events/twjug202010

LINE Developers Taiwan

October 21, 2020
Tweet

More Decks by LINE Developers Taiwan

Other Decks in Technology

Transcript

  1. :K\PLJUDWLQJWR-8QLW • 6XSSRUWHGLQPDMRU,'(%XLOGWRROV • ,QWHOOLM ,'($(FOLSVH96&RGH • 0DYHQ*UDGOH • (DV\PLJUDWLRQ

    • -8QLWWHVWVFDQFRH[LVWZLWK-8QLWWHVWV • :HFDQXVHERWK-8QLW$VVHUW- DVVHUWLRQVLQRQHWHVW • 1HZ)HDWXUHV
  2. 1HVWHGWHVWV • #1HVWHGFDQKDYHPXOWLSOHOHYHOV • 6XSSRUWQRQVWDWLFLQQHUFODVV class TestingAStackDemo { Stack<Object> stack;

    @Test void isInstantiatedWithNew() { new Stack<>(); } @Nested class WhenNew { @BeforeEach void createNewStack() { stack = new Stack<>(); } @Test void isEmpty() { assertTrue(stack.isEmpty()); } @Test void throwsExceptionWhenPopped() { assertThrows(EmptyStackException.class, stack::pop); } @Nested class AfterPushing { // more tests... } } }
  3. 'LVSOD\QDPHV • $GG#'LVSOD\1DPH DQQRWDWLRQ • 6XSSRUWDGGLQJRQFODVVPHWKRG @DisplayName("A stack") class TestingAStackDemo

    { Stack<Object> stack; @Test @DisplayName("is instantiated with new Stack()") void isInstantiatedWithNew() { new Stack<>(); } @Nested @DisplayName("when new") class WhenNew { @BeforeEach void createNewStack() { stack = new Stack<>(); } @Test @DisplayName("is empty") void isEmpty() { assertTrue(stack.isEmpty()); } @Nested @DisplayName("after pushing an element") class AfterPushing { // more tests... } } }
  4. 3DUDPHWHUL]HGWHVWV -8QLW class CalculatorJUnit5Test { @ParameterizedTest @ValueSource(strings = { "",

    " ", " " }) void isBlank_ShouldReturnTrueForBlankStrings(String input) { assertThat(StringUtils.isBlank(input)).isTrue(); } @ParameterizedTest @NullSource void isBlank_ShouldReturnTrueForNullStrings(String input) { assertThat(StringUtils.isBlank(input)).isTrue(); } @ParameterizedTest @CsvSource({ "0,0,0", "1,1,2" }) void add(int first, int second, int expected) { final Calculator calculator = new Calculator(); final int result = calculator.add(first, second); assertThat(result).isEqualTo(expected); } }
  5. 0XOWLSOH5XQQHUV -8QLWRQO\VXSSRUWVVLQJOHUXQQHUZKLOH-8QLWVXSSRUWVPXOWLSOHH[WHQVLRQV // JUnit 4 @RunWith(MockitoJUnitRunner.class) public class CMSProductServiceTest {

    // ... } // JUnit 5 @ExtendWith({ MockitoExtension.class, SoftAssertionsExtension.class }) public class CMSProductServiceTest { // ... }
  6. 0LJUDWHH[LVWLQJ-8QLWWHVWV JUnit 4 JUnit 5 org.junit.Test org.junit.jupiter.api.Test org.junit.Before org.junit.jupiter.api.BeforeEach org.junit.After

    org.junit.jupiter.api.AfterEach org.junit.BeforeClass org.junit.jupiter.api.BeforeAll org.junit.AfterClass org.junit.jupiter.api.AfterAll org.junit.Ignore org.junit.jupiter.api.Disabled org.junit.Assert org.junit.jupiter.api.Assertions (we use AssertJ) org.junit.Assert.assertThat org.hamcrest.MatcherAssert.assertThat org.junit.runner.RunWith org.junit.jupiter.api.extension.ExtendWith org.mockito.junit.MockitoJUnitRunner org.mockito.junit.jupiter.MockitoExtension org.springframework.test.context.junit4.SpringRu nner org.springframework.test.context.junit.jupiter.Spri ngExtension
  7. 5HZULWH-8QLW5XOHWR-8QLW([WHQVLRQ -8QLW5XOH public class EmbeddedMySqlRule extends ExternalResource { private EmbeddedMysql

    embeddedMysql; @Override protected void before() { // ... embeddedMysql = EmbeddedMysql.anEmbeddedMysql(config, downloadConfig) .addSchema("ecac", ScriptResolver.classPathScript("db/setup.sql")) .start(); } @Override protected void after() { if (embeddedMysql != null) { embeddedMysql.stop(); } } }
  8. 5HZULWH-8QLW5XOHWR-8QLW([WHQVLRQ -8QLW([WHQVLRQ public class EmbeddedMySqlExtension implements AfterAllCallback, BeforeAllCallback { private

    EmbeddedMysql embeddedMysql; @Override public void beforeAll(ExtensionContext extensionContext) throws Exception { // ... embeddedMysql = EmbeddedMysql.anEmbeddedMysql(config, downloadConfig) .addSchema("ecac", ScriptResolver.classPathScript("db/setup.sql")) .start(); } @Override public void afterAll(ExtensionContext extensionContext) throws Exception { if (embeddedMysql != null) { embeddedMysql.stop(); } } }