Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit5 TestReporter

Tags:

java

junit5

I was trying understand TestReporter in Junit5

@BeforeEach
void beforeEach(TestInfo testInfo) {

}

@ParameterizedTest
@ValueSource(strings = "foo")
void testWithRegularParameterResolver(String argument, TestReporter testReporter) {
    testReporter.publishEntry("argument", argument);
}

@AfterEach
void afterEach(TestInfo testInfo) {
    // ...
}

what is the use of publishEntry in TestReporter,

Can someone explain me.. Thanks in Advance..

like image 752
Karunakar Reddy L Avatar asked Jun 11 '26 05:06

Karunakar Reddy L


2 Answers

"TestReporter" in conjunction with "TestInfo" gives an instance of the current test, this way you can get info about your actual test. and then publish it, in this example used as kind of logger.

StringBuffer is used for his mutable, fast, and synchonized characteristics, required for a test.

public class TestReporterTest {
    StringBuffer sbtags = new StringBuffer();
    StringBuffer displayName = new StringBuffer();
    StringBuffer className = new StringBuffer();
    StringBuffer methodName = new StringBuffer();

    @BeforeEach
    void init(TestInfo testInfo) {
        className.delete( 0, className.length());
        className.append( testInfo.getTestClass().get().getName());
        displayName.delete( 0, displayName.length());
        displayName.append( testInfo.getDisplayName());
        methodName.delete( 0, methodName.length());
        methodName.append( testInfo.getTestMethod().get().getName());
    }

    @Test
    @DisplayName("testing on reportSingleValue")
    void reportSingleValue(TestReporter testReporter) {
        testReporter.publishEntry( "className  : " + className);
        testReporter.publishEntry( "displayName: " + displayName);
        testReporter.publishEntry("methodName  : " + methodName);
        testReporter.publishEntry("algun mensaje de estatus");
    }

    @Test
    void reportKeyValuePair(TestReporter testReporter) {
        testReporter.publishEntry( "className  : " + className);
        testReporter.publishEntry( "displayName: " + displayName);
        testReporter.publishEntry("methodName  : " + methodName);
        testReporter.publishEntry("una Key", "un Value");
    }

    @Test
    void reportMultiKeyValuePairs(TestReporter testReporter) {
        Map<String, String> map = new HashMap<>();
        map.put("Fast and Furious 8","2018");
        map.put("Matrix","1999");

        testReporter.publishEntry( "className  : " + className);
        testReporter.publishEntry( "displayName: " + displayName);
        testReporter.publishEntry("methodName  : " + methodName);
        testReporter.publishEntry(map);
    }
}

Running the Test

timestamp = 2019-11-22T12:02:45.898, value = className  : TestReporterTest
timestamp = 2019-11-22T12:02:45.904, value = displayName: testing on reportSingleValue
timestamp = 2019-11-22T12:02:45.904, value = methodName  : reportSingleValue
timestamp = 2019-11-22T12:02:45.904, value = algun mensaje de estatus


timestamp = 2019-11-22T12:02:45.919, value = className  : TestReporterTest
timestamp = 2019-11-22T12:02:45.920, value = displayName: reportMultiKeyValuePairs(TestReporter)
timestamp = 2019-11-22T12:02:45.920, value = methodName  : reportMultiKeyValuePairs
timestamp = 2019-11-22T12:02:45.921, Fast and Furious 8 = 2018, Matrix = 1999


timestamp = 2019-11-22T12:02:45.924, value = className  : TestReporterTest
timestamp = 2019-11-22T12:02:45.925, value = displayName: reportKeyValuePair(TestReporter)
timestamp = 2019-11-22T12:02:45.925, value = methodName  : reportKeyValuePair
timestamp = 2019-11-22T12:02:45.925, una Key = un Value
like image 134
jalbertomr Avatar answered Jun 13 '26 19:06

jalbertomr


Apart from the previous answers, When we are writing junit test scripts if we want to get some information out of the process we normally do System.out.println which is not recommended in corporate/enterprise world. Specially in code reviews, peer reviews we are advised to remove all the System.out.println from the code base. So in the junit world if we want to push or publish out of the scripts we are advised to use TestReporter publishEntry() method. With the combination of TestInfo we could read several information out of the original junit scripts.

Hope this facts also support your question.

like image 28
Isuru Dewasurendra Avatar answered Jun 13 '26 18:06

Isuru Dewasurendra