0

I run according to the documentation with a test class with 30 tests inside org.junit.platform.launcher.Launcher

I have a function launcher.registerTestExecutionListeners(listener) registered in org.junit.platform.launcher.listeners.TestExecutionSummary

I read the following and don't know how to use it: https://junit.org/junit5/docs/current/user-guide/#running-tests-listeners https://junit.org/junit5/docs/current/api/org.junit.platform.launcher/org/junit/platform/launcher/TestExecutionListener.html

I need listeners: progress - what test is currently running, are there any errors printed, etc.

For example this one:

val listener: SummaryGeneratingListener = SummaryGeneratingListener()
launcher.registerTestExecutionListeners(listener)
val summary: TestExecutionSummary = listener.summary
val failed: Long = summary.testsFailedCount

I need commands: stop/pause/exit/skip, etc.

I need code examples on how to use it. Can anyone help with links?

The next question is asked here: How to terminate the application and make the launcher return to its call by the stop command?

1
  • Welcome to the community. What would help you get better answers to your question is to see what you have done to solve this. What does your code look like? Does it work or not? What errors do you get?
    – Lee Jensen
    Feb 13 at 21:33

1 Answer 1

1

You can create your own custom listener that implements the TestExecutionListener interface and perform custom actions in response to certain events. However, you would need to implement the logic for pausing, exiting, or skipping tests yourself.

This is a simplistic example of how you can create a custom listener:

import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;

public class CustomTestExecutionListener implements TestExecutionListener {

  private boolean shouldPause = false;
  private boolean shouldExit = false;
  private boolean shouldSkip = false;

  @Override
  public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
    System.out.println("Finished test: " + testIdentifier.getDisplayName());
    if (shouldPause) {
      System.out.println("Pausing for 5 seconds");
      try {
        TimeUnit.SECONDS.sleep(5);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      shouldPause = false;
    }
    if (shouldExit) {
      System.out.println("Exiting test execution");
      System.exit(0);
    }
  }

  public void setShouldPause(boolean shouldPause) {
    this.shouldPause = shouldPause;
  }

  public void setShouldExit(boolean shouldExit) {
    this.shouldExit = shouldExit;
  }

  public void setShouldSkip(boolean shouldSkip) {
    this.shouldSkip = shouldSkip;
  }
}

You can then register the custom listener with the launcher using the registerTestExecutionListeners method:

CustomTestExecutionListener listener = new CustomTestExecutionListener();
launcher.registerTestExecutionListeners(listener);

and set the flags for pausing, exiting, or skipping tests as needed:

listener.setShouldPause(true);
listener.setShouldExit(true);
listener.setShouldSkip(true);
1
  • 1
    Thank you. It works. I managed to solve: 1. Now pause when starting or stopping the test 2. Now there is information about what is done inside the class at the end of each test from the class I will publish the result here a little later, because I'm still working on it. And there are still questions. Thanks again for the efficient answer.
    – DinaD
    Feb 14 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.