Thursday, March 28, 2024
HomeJavaProgrammatically Take heed to JUnit Take a look at Outcomes - Java...

Programmatically Take heed to JUnit Take a look at Outcomes – Java Code Geeks


There are numerous methods accessible to run JUnit exams, from the IDE, construct device, or improvement modes. To course of and use the check outcomes, nonetheless, one has to both learn the output or parse end result recordsdata (such because the XML surefire stories). It’s additionally potential to outline end result listeners programmatically in JUnit.

You may register a customized TestExecutionListener, that will probably be notified at sure occasions that occur throughout testing, comparable to when the entire check plan in addition to particular person executions begin or cease. We outline our personal implementation of this — TestResultNotifier — that gathers and makes use of the general end result. This class can reside below src/check/java, as another check class:

import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;

public class TestResultNotifier implements TestExecutionListener {

    non-public boolean handed = true;

    @Override
    public void executionFinished(TestIdentifier testIdentifier,
            TestExecutionResult testExecutionResult) {

        if (testExecutionResult.getStatus() == TestExecutionResult.Standing.FAILED)
            handed = false;
    }

    @Override
    public void testPlanExecutionFinished(TestPlan testPlan) {
        if (!handed) {
            System.out.println("!!!");
            System.out.println("Exams NOT handed!");
            System.out.println("!!!");
        } else {
            System.out.println("All exams handed!");
        }
    }

}

With a view to mechanically register the JUnit listener, we add a file below src/check/sources/META-INF/providers/ named org.junit.platform.launcher.TestExecutionListener. The Content material of the file ought to level to our class, so a single line containing:

com.sebastian_daschner.espresso.TestResultNotifier

Then, we are able to execute the exams in any means we like, and we are going to see the output. The next exhibits the Maven output for the instance mission:

[...]
[INFO] --- maven-surefire-plugin:2.22.2:check (default-test) @ quarkus-playground ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Operating com.sebastian_daschner.espresso.CoffeeShopTest
[ERROR] Exams run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.098 s <<< FAILURE! - in com.sebastian_daschner.espresso.CoffeeShopTest
[ERROR] check  Time elapsed: 0.028 s  <<< FAILURE!
org.opentest4j.AssertionFailedError:

Anticipating:
 <false>
to be equal to:
 <true>
however was not.
        at com.sebastian_daschner.espresso.CoffeeShopTest.check(CoffeeShopTest.java:18)

!!!
Exams NOT handed!
!!!
[INFO]
[INFO] Outcomes:
[INFO]
[...]

Nice, this already works. Now, you may rightfully ask what’s the purpose.

Excessive Suggestions System Instance

Nicely, a System.out instance after all is boring, however now now we have all prospects of the Java world to regionally react to the check outcomes, each single run. For instance, for some advanced UI check suites that run for an extended time, I’m utilizing an “excessive suggestions” machine, comparable to a blinking LED (the blink(1) USB LED to be exact) to inform me.

To get such an integration, you possibly can code some connection in Java, or additionally name a command line executable:

@Override
public void testPlanExecutionFinished(TestPlan testPlan) {
    String arg = handed ? "--green" : "--red";

    strive {
        Course of course of = new ProcessBuilder("blink1-tool", arg).begin();
        course of.waitFor();
    } catch (IOException | InterruptedException e) {
        System.err.println("Couldn't execute blink1-tool");
        e.printStackTrace();
    }
}

The blink1-tool is an executable that controls the LED, and in our case it is going to both blink purple or inexperienced. Now, the cool factor is that this reacts fairly rapidly, so in the event you run your exams by way of a improvement mode comparable to quarkus:dev with steady testing, you will notice the outcomes even earlier than you rapidly swap the window or look on the command line. Should you’re , I can create a brief video that demos this.

You may take a look at the instance mission on GitHub.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments