Friday 20 August 2021

Junit5: TestReporterParameterResolver: Get data about current test run

If a constructor or method parameter is of type TestReporter, the TestReporterParameterResolver will supply an instance of TestReporter. The TestReporter can be used to publish additional data about the current test run.

 

Find the below working applications.

 

TestReporterDemo.java

package com.sample.app;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;

public class TestReporterDemo {
	@Test
	void reportSingleValue(TestReporter testReporter) {
		System.out.println("\nInside reportSingleValue method");
		testReporter.publishEntry("a status message");
	}

	@Test
	void reportKeyValuePair(TestReporter testReporter) {
		System.out.println("\nInside reportKeyValuePair method");
		testReporter.publishEntry("methodName", "reportKeyValuePair");
	}

	@Test
	void reportMultipleKeyValuePairs(TestReporter testReporter) {
		System.out.println("\nInside reportMultipleKeyValuePairs method");
		
		Map<String, String> values = new HashMap<>();
		testReporter.publishEntry("methodName", "reportMultipleKeyValuePairs");
		testReporter.publishEntry("demo", "TestReporter");

		testReporter.publishEntry(values);
	}
}

Run above test class, you will see below messages in console.

Inside reportSingleValue method
TestIdentifier [reportSingleValue(TestReporter)]
ReportEntry [timestamp = 2020-07-04T12:35:26.408, value = 'a status message']

Inside reportMultipleKeyValuePairs method
TestIdentifier [reportMultipleKeyValuePairs(TestReporter)]
ReportEntry [timestamp = 2020-07-04T12:35:26.416, methodName = 'reportMultipleKeyValuePairs']
TestIdentifier [reportMultipleKeyValuePairs(TestReporter)]
ReportEntry [timestamp = 2020-07-04T12:35:26.416, demo = 'TestReporter']
TestIdentifier [reportMultipleKeyValuePairs(TestReporter)]
ReportEntry [timestamp = 2020-07-04T12:35:26.416]

Inside reportKeyValuePair method
TestIdentifier [reportKeyValuePair(TestReporter)]
ReportEntry [timestamp = 2020-07-04T12:35:26.418, methodName = 'reportKeyValuePair']


Junit test report window is given below.




 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment