user1677663
user1677663

Reputation: 1461

What's the equivalent of org.junit.runner.JUnitCore.runClasses in Junit 5?

The following code started in JUnit4 and has been mostly translated into JUnit5 except for main(). The reason I'm writing it this way is that I'm demonstrating TDD and I have multiple versions of the StringInverter implementation, each of which implements more features and passes more tests. Here is the StringInverter interface:

interface StringInverter {
  public String invert(String str);
}

And here's the almost-compiling-with-JUnit5 class:

import java.util.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.platform.runner.JUnitPlatform;

public class StringInverterTest {
  static StringInverter inverter;
  @Test
  public final void basicInversion_Succeed() {
    String in = "Exit, Pursued by a Bear.";
    String out = "eXIT, pURSUED BY A bEAR.";
    assertEquals(inverter.invert(in), out);
  }
  @Test
  public final void basicInversion_Fail() {
    expectThrows(RuntimeException.class, () -> {
      assertEquals(inverter.invert("X"), "X");
    });
  }
  @Test
  public final void allowedCharacters_Fail() {
    expectThrows(RuntimeException.class, () -> {
      inverter.invert(";-_()*&^%$#@!~`");
      inverter.invert("0123456789");
    });
  }
  @Test
  public final void allowedCharacters_Succeed() {
    inverter.invert("abcdefghijklmnopqrstuvwxyz ,.");
    inverter.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
  }
  @Test
  public final void lengthLessThan26_Fail() {
    String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
    assertTrue(str.length() > 25);
    expectThrows(RuntimeException.class, () -> {
      inverter.invert(str);
    });
  }
  @Test
  public final void lengthLessThan26_Succeed() {
    String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
    assertTrue(str.length() < 26);
    inverter.invert(str);
  }
  public static void main(String[] args) throws Exception{
    assertEquals(args.length, 1);
    inverter = (StringInverter)
      Class.forName(args[0]).newInstance();
    Result result = org.junit.runner.JUnitCore.runClasses(
      StringInverterTest.class);
    List<Failure> failures = result.getFailures();
    System.out.printf("%s has %d FAILURES:\n",
      args[0], failures.size());
    int count = 1;
    for(Failure f : failures) {
      System.out.printf("Failure %d:\n", count++);
      System.out.println(f.getDescription());
      System.out.println(f.getMessage());
    }
  }
}

main() worked with JUnit4, so my question is how to convert it to JUnit 5. Thanks!

Upvotes: 11

Views: 9221

Answers (2)

ttddyy
ttddyy

Reputation: 181

JUnit5 has launcher API in junit-platform-launcher module which is for programmatic test discovery and execution.

Detailed example is documented on their user guide chapter7.

Upvotes: 13

Valentyn Kolesnikov
Valentyn Kolesnikov

Reputation: 2097

I found a solution how to start unit tests from main static method.

Dependencies:

org.junit.jupiter:junit-jupiter-api:5.2.0
org.junit.platform:junit-platform-launcher:1.2.0
org.junit.jupiter:junit-jupiter-engine:5.2.0

Code example:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.function.Executable;

import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherFactory;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
import org.junit.platform.launcher.listeners.TestExecutionSummary.Failure;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
    final LauncherDiscoveryRequest request = 
        LauncherDiscoveryRequestBuilder.request()
                                   .selectors(selectClass(MyClass.class))
                                   .build();

        final Launcher launcher = LauncherFactory.create();
        final SummaryGeneratingListener listener = new SummaryGeneratingListener();

        launcher.registerTestExecutionListeners(listener);
        launcher.execute(request);

        TestExecutionSummary summary = listener.getSummary();
        long testFoundCount = summary.getTestsFoundCount();
        List<Failure> failures = summary.getFailures();
        System.out.println("getTestsSucceededCount() - " + summary.getTestsSucceededCount());
        failures.forEach(failure -> System.out.println("failure - " + failure.getException()));
    }

    @Test
    void exceptionTesting() {
        Executable closureContainingCodeToTest = () -> {throw new IllegalArgumentException("a message");};
        Throwable throwable = assertThrows(IllegalArgumentException.class, closureContainingCodeToTest, "a message");
        assertEquals("a message", throwable.getMessage());
    }
}

Upvotes: 15

Related Questions