runClasses等效于什么

runClasses等效于什么

本文介绍了Junit 5中的org.junit.runner.JUnitCore.runClasses等效于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码始于JUnit4,除main()之外,大部分已转换为JUnit5.我用这种方式编写代码的原因是,我正在演示TDD,并且我有多个版本的StringInverter实现,每个版本都实现了更多功能并通过了更多测试.这是StringInverter界面:

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);
}

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

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()与JUnit4一起使用,所以我的问题是如何将其转换为JUnit5.谢谢!

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

推荐答案

JUnit5在 junit-platform-launcher 模块,用于程序化测试发现和执行.

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

详细示例记录在其用户指南第7 章中

这篇关于Junit 5中的org.junit.runner.JUnitCore.runClasses等效于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:03