From efcef3152a605a9401cfd5eb22178c7e54fca8f1 Mon Sep 17 00:00:00 2001 From: Yashasvii Date: Wed, 19 Jan 2022 15:05:19 +0545 Subject: [PATCH] BAEL-5208: Get the name of the currently executing test in JUnit (#11462) * Hexagonal Architecture in Java * Refactor Hexagonal Architecture in Java * Refactor Hexagonal Architecture in Java * BAEL-5163: HashMap - keySet vs entrySet vs values methods * BAEL-5163: HashMap - keySet vs entrySet vs values methods * Revert "Hexagonal Architecture in Java" This reverts commit 1add21c1 * BAEL-5163: HashMap - keySet vs entrySet vs values methods * BAEL-5163: HashMap - keySet vs entrySet vs values methods" * BAEL-5208: Get the name of the currently executing test in JUnit * BAEL-5208: Get the name of the currently executing test in JUnit * BAEL-5208: Get the name of the currently executing test in JUnit * BAEL-5208: Get the name of the currently executing test in JUnit * BAEL-5208: Get the name of the currently executing test in JUnit * BAEL-5208: Get the name of the currently executing test in JUnit --- .../JUnit4ParameterizedTestNameUnitTest.java | 44 +++++++++++++++++++ .../JUnit4SimpleTestNameUnitTest.java | 28 ++++++++++++ .../JUnit5SimpleTestNameUnitTest.java | 20 +++++++++ .../Junit5ParameterizedTestNameUnitTest.java | 29 ++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit4ParameterizedTestNameUnitTest.java create mode 100644 testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit4SimpleTestNameUnitTest.java create mode 100644 testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit5SimpleTestNameUnitTest.java create mode 100644 testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/Junit5ParameterizedTestNameUnitTest.java diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit4ParameterizedTestNameUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit4ParameterizedTestNameUnitTest.java new file mode 100644 index 0000000000..ceb31a6387 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit4ParameterizedTestNameUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.gettestname; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; + +@RunWith(Parameterized.class) +public class JUnit4ParameterizedTestNameUnitTest { + + private String input; + private String expected; + + @Rule + public TestName name = new TestName(); + + public JUnit4ParameterizedTestNameUnitTest(String input, String expected) { + this.input = input; + this.expected = expected; + } + + @Parameterized.Parameters(name = "{0}") + public static Collection suppliedData() { + return Arrays.asList(new Object[][] { { "abc", "abc" }, { "cba", "abc" }, { "onm", "mno" }, { "a", "a" }, { "zyx", "xyz" }, }); + } + + private static String sortCharacters(String s) { + char[] charArray = s.toCharArray(); + Arrays.sort(charArray); + return new String(charArray); + } + + @Test + public void givenString_whenSort_thenVerifySortForString() { + System.out.println("displayName = " + name.getMethodName()); + assertEquals(expected, sortCharacters(input)); + } +} diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit4SimpleTestNameUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit4SimpleTestNameUnitTest.java new file mode 100644 index 0000000000..0ce955f1ec --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit4SimpleTestNameUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.gettestname; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +public class JUnit4SimpleTestNameUnitTest { + + @Rule + public TestName name = new TestName(); + + private static String sortCharacters(String s) { + char[] charArray = s.toCharArray(); + Arrays.sort(charArray); + return new String(charArray); + } + + @Test + public void givenString_whenSort_thenVerifySortForString() { + System.out.println("displayName = " + name.getMethodName()); + String s = "abc"; + assertEquals(s, sortCharacters("cba")); + } +} diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit5SimpleTestNameUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit5SimpleTestNameUnitTest.java new file mode 100644 index 0000000000..57ab253312 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/JUnit5SimpleTestNameUnitTest.java @@ -0,0 +1,20 @@ +package com.baeldung.gettestname; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class JUnit5SimpleTestNameUnitTest { + + private boolean oddCheck(int number) { + return (number % 2 != 0); + } + + @Test + void givenNumbers_whenOddCheck_thenVerify(TestInfo testInfo) { + System.out.println("displayName = " + testInfo.getDisplayName()); + int number = 5; + assertTrue(oddCheck(number)); + } +} diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/Junit5ParameterizedTestNameUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/Junit5ParameterizedTestNameUnitTest.java new file mode 100644 index 0000000000..ecce0abe34 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/gettestname/Junit5ParameterizedTestNameUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.gettestname; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class Junit5ParameterizedTestNameUnitTest { + + private TestInfo testInfo; + + @BeforeEach + void init(TestInfo testInfo) { + this.testInfo = testInfo; + } + + private boolean oddCheck(int number) { + return (number % 2 != 0); + } + + @ParameterizedTest(name = "givenNumbers_whenOddCheck_thenVerify{0}") + @ValueSource(ints = { 1, 3, 5, -3, 15 }) + void givenNumbers_whenOddCheck_thenVerify(int number) { + System.out.println("displayName = " + testInfo.getDisplayName()); + assertTrue(oddCheck(number)); + } +}