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
This commit is contained in:
Yashasvii 2022-01-19 15:05:19 +05:45 committed by GitHub
parent caa13dd4d1
commit efcef3152a
4 changed files with 121 additions and 0 deletions

View File

@ -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<Object[]> 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));
}
}

View File

@ -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"));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}