How to get hold of RunNotifier while migrating a JUnit4 custom runner to JUnit5

时间:2019-04-17 01:01:46

标签: java testing junit5 junit5-extension-model

I am in the process of writing an equivalent JUnit4 runner in JUnit5. I was advised to use @ExtendWith model.

But while implementing the old JUnit4 runners using new JUnit5 extension, I encountered this problem. i.e. there is no RunNotifier notifier available in the ExtensionContext.

In our tests engine/runner, we read the @MyTestResource("test/withdrawal1_dsl.json") and after processing the data, we validate the result. Then if the outcome(multiple fields) don't match the expectations((multiple fields), we collect the failure messages and then fail the test by invoking

notifier.fireTestFailure(new Failure(description, new RuntimeException( customMultipleFailureMsgs)));

Example code looks like below-

@Override
    protected void runChild(FrameworkMethod method, RunNotifier notifier) {
        notifier.fireTestStarted(description);

        final Description description = describeChild(method);
        MyTestResource annotation = method.getMethod().getAnnotation(MyTestResource.class);
        ...
        ...
        // Here we read this "test/withdrawal1_dsl.json" via @MyTestResource 
        // and process the desired operation described in the JSON file.


        LOGGER.debug("### Running test : " + currentTestCase);

        MyTest child = null;
        try {
            child = smartUtils.jsonFileToJava(currentTestCase, MyTest.class);
            // runTest(...) method does the business logic and collects all failures, 
            // then calls "notifier.firetestFailure(...)" 
            passed = myTestProcessor.runTest(child, notifier, description);

        } catch (Exception ex) {
            notifier.fireTestFailure(new Failure(description, ex));
        }

        if(!passed){
            notifier.fireTestFailure(new Failure(description, new Exception(failedMsgs)));
        }    

        notifier.fireTestFinished(description);
    }

The sample MyCustomRunner snippet is here for reference.

How to get hold of the RunNotifier notifier in JUnit5?

Or is there any other approach to solve this bit?

Appreciate if there are any examples/sample code please.

0 个答案:

没有答案