如何从命令行(没有Maven/Gradle)启动JUnit 5(平台)?

2022-09-02 03:21:49

我想从命令行运行一个包含JUnit 5测试的类。不幸的是,我有一些外部依赖项阻止我使用Maven,Gradle或其他构建系统。

在JUnit 4中,我可以像这样完成此操作

java .:"lib/*" org.junit.runner.JUnitCore TestClass

JUnit 5 是否有等效项?我只是想知道测试是否成功,类似于它在IntelliJ中运行时。

TestClass.java

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.*;

public class TestClass {

    private static ArrayList<Student> students;
    private static ArrayList<Student> inAgeOrderStudents;
    private static ArrayList<Student> inNameOrderStudents;

    @BeforeAll
    static void setUp(){
        initializeStudents();
        initSortedAgeStudents();
        initSortedNameStudents();
    }

    @BeforeEach
    void reloadStudents() {
        Collections.shuffle(students);
    }

   static void initializeStudents(){
        students = new ArrayList<Student>();

        students.add(new Student(18, "Tim"));
        students.add(new Student(18, "Tim"));
        students.add(new Student(16, "Jean"));
        students.add(new Student(14, "Lin"));
        students.add(new Student(19, "Sam"));
    }

    static void initSortedAgeStudents(){
        inAgeOrderStudents = new ArrayList<Student>();
        inAgeOrderStudents.add(new Student(14, "Lin"));
        inAgeOrderStudents.add(new Student(16, "Jean"));
        inAgeOrderStudents.add(new Student(18, "Tim"));
        inAgeOrderStudents.add(new Student(18, "Tim"));
        inAgeOrderStudents.add(new Student(19, "Sam"));
    }

    static void initSortedNameStudents(){
        inNameOrderStudents = new ArrayList<Student>();
        inNameOrderStudents.add(new Student(16, "Jean"));
        inNameOrderStudents.add(new Student(14, "Lin"));
        inNameOrderStudents.add(new Student(19, "Sam"));
        inNameOrderStudents.add(new Student(18, "Tim"));
        inNameOrderStudents.add(new Student(18, "Tim"));
    }



    @Test
    void testMergeSort() {
        assertNotEquals(students, inAgeOrderStudents);
        StudentSortSearch.mergesort(students,StudentSortSearch.SortSearchCriteria.AGE);
        assertEquals(14,students.get(0).getAge());
        assertEquals(19,students.get(4).getAge());
        assertEquals(students, inAgeOrderStudents);

        assertEquals(true,students.equals(inAgeOrderStudents));
    }

    @Test
    void testQuickSort() {
        StudentSortSearch.quickSort(students,StudentSortSearch.SortSearchCriteria.NAME);
        assertEquals("Jean",students.get(0).getName());
        assertEquals("Tim",students.get(4).getName());

        assertEquals(students, inNameOrderStudents);
    }

    @Test
    void testBinarySearch() {
        StudentSortSearch searcher = new StudentSortSearch();
        ArrayList<Student> searchResults = searcher.binarySearch(students, 18);
        assertEquals(2, searchResults.size());
        assertEquals(18,searchResults.get(1).getAge());
        assertEquals(18,searchResults.get(0).getAge());

        searchResults = searcher.binarySearch(students, "Lin");
        assertEquals(1, searchResults.size());
        assertEquals(14,searchResults.get(0).getAge());
    }
}

答案 1

当然,请使用 .ConsoleLauncher

是一个命令行 Java 应用程序,允许您从控制台启动 JUnit 平台。例如,它可用于运行 JUnit VintageJUnit Jupiter 测试,并将测试执行结果打印到控制台。ConsoleLauncher

包含所有依赖项的可执行文件发布在中央 Maven 存储库的 junit-platform-console-standalone 目录下。您可以运行独立的控制台启动器,如下所示。*junit-platform-console-standalone-<version>.jar*

java -jar junit-platform-console-standalone-<version>.jar <Options>

有关选项的详细信息,请咨询 https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher

根据您的示例量身定制并使用 JUnit 平台版本 ,命令可能如下所示:1.3.1

$ mkdir out
$ javac -d out Student.java StudentSortSearch.java
$ javac -d out -cp out:junit-platform-console-standalone-1.3.1.jar TestClass.java
$ java -jar junit-platform-console-standalone-1.3.1.jar --class-path out --scan-class-path
╷
├─ JUnit Jupiter ✔
│  └─ TestClass ✔
│     └─ test() ✔
└─ JUnit Vintage ✔

Test run finished after 67 ms
...

答案 2

推荐