Kannan Ekanath
Kannan Ekanath

Reputation: 17621

JUnit5 - customised lifecycle events/extension points

I am writing an acceptance test suite in Junit5. I'd like to generate customised output (html) specific to my domain as a result of this. This output will contain data that is obtained from lifecycle events such as test started (or before all tests) and also some data comes from non-lifecycle events - for example: in the middle of the execution of a test I'd like to emit some data that is also collected for reporting.

Can someone tell me how to do this? I am aware of custom listeners and extensions which are all good for lifecycle events however it is not easy to add some non-lifecycle data in the middle of test execution around this?

Upvotes: 0

Views: 49

Answers (1)

beatngu13
beatngu13

Reputation: 9393

As @johanneslink already pointed out, you can use TestReporter "to publish report entries for the current container or test to the reporting infrastructure", also in the middle of a test execution:

class TestReporterDemo {

    @Test
    void reportSingleValue(TestReporter testReporter) {
        testReporter.publishEntry("a status message");
    }

    @Test
    void reportKeyValuePair(TestReporter testReporter) {
        testReporter.publishEntry("a key", "a value");
    }

    @Test
    void reportMultipleKeyValuePairs(TestReporter testReporter) {
        Map<String, String> values = new HashMap<>();
        values.put("user name", "dk38");
        values.put("award year", "1974");

        testReporter.publishEntry(values);
    }

}

Or you create a custom ParamterResolver for your own reporter/extension implementation. See also: "Dependency Injection for Constructors and Methods".

Upvotes: 0

Related Questions