9

以下代码从 JUnit4 开始,除了main(). 我这样写的原因是我正在演示 TDD,并且我有多个版本的StringInverter实现,每个版本都实现了更多的功能并通过了更多的测试。这是StringInverter界面:

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

这是几乎与 JUnit5 一起编译的类:

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()使用 JUnit4,所以我的问题是如何将其转换为 JUnit 5。谢谢!

4

2 回答 2

11

我找到了一个解决方案,如何从主要静态方法开始单元测试。

依赖项:

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

代码示例:

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());
    }
}
于 2018-07-23T09:25:05.027 回答
11

JUnit5 在junit-platform-launcher模块中有启动器 API,用于编程测试发现和执行。

详细示例记录在他们的用户指南第 7 章中。

于 2016-08-24T03:57:54.520 回答