简体   繁体   English

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

[英]How to launch JUnit 5 (Platform) from the command line (without Maven/Gradle)?

I'd like to run a class containing JUnit 5 tests from the command line. 我想从命令行运行一个包含JUnit 5测试的类。 Unfortunately, I have some outside dependencies that prevent me from using Maven, Gradle, or other build systems. 不幸的是,我有一些外部依赖项阻止我使用Maven,Gradle或其他构建系统。

In JUnit 4, I could accomplish this like 在JUnit 4中,我可以完成这个

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

Is there an equivalent for JUnit 5? JUnit 5有相同的功能吗? I'd simply like to know if test succeeded similar to when it runs in IntelliJ. 我只想知道测试是否成功与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());
    }
}

Sure, use the ConsoleLauncher . 当然,使用ConsoleLauncher

The ConsoleLauncher is a command-line Java application that lets you launch the JUnit Platform from the console. ConsoleLauncher是一个命令行Java应用程序,允许您从控制台启动JUnit平台 For example, it can be used to run JUnit Vintage and JUnit Jupiter tests and print test execution results to the console. 例如,它可用于运行JUnit VintageJUnit Jupiter测试并将测试执行结果打印到控制台。

An executable *junit-platform-console-standalone-<version>.jar* with all dependencies included is published in the central Maven repository under the junit-platform-console-standalone directory. 包含所有依赖项的可执行文件*junit-platform-console-standalone-<version>.jar*发布在junit-platform-console-standalone目录下的中央Maven存储库中。 You can run the standalone ConsoleLauncher as shown below. 您可以运行独立的ConsoleLauncher,如下所示。

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

For details about the options consult https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher please. 有关选项的详细信息,请参阅https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher

Tailored to your example and using JUnit Platform version 1.3.1 , the commands could look like those: 根据您的示例量身定制并使用JUnit Platform 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
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从jUnit测试启动maven过滤? - How to launch maven filtering from jUnit test? Maven在Eclipse中运行JUnit测试,但从命令行编译失败 - Maven runs JUnit tests in Eclipse but compilation fails from the command line 从命令行将JUnit Categories赛跑者与matchAny一起使用以及Maven Surefire - Using JUnit Categories runner with matchAny with Maven Surefire from command line Maven 项目中的 JUnit 5 测试适用于 IntelliJ,但不适用于命令行 - JUnit 5 tests in Maven Project works in IntelliJ but Not from Command Line 无法从 maven 命令行调用 cucumber junit 测试 - Unable to invoke cucumber junit test from maven command line 在没有Maven或Gradle的情况下将JUnit 5与Java 9结合使用 - Using JUnit 5 with Java 9 without Maven or Gradle 如何在命令行中运行Eclipse + Maven + JUnit测试 - How to Run Eclipse+Maven+JUnit tests in Command Line 如何使用本地 JUnit Jar 从 Gradle 而不是 Maven 中央? - How to use local JUnit Jar from Gradle instead of Maven Central? 如何通过命令行运行Maven / Gradle Web项目? - How to run a Maven / Gradle web project through command line? 如何在不模拟的情况下从 JUnit 测试命令行 Java 应用程序? - How can I test a command line Java app from JUnit, without mocking?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM