• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

用户头像
it1352
帮助1

问题说明

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

I'd like to run a class containing JUnit 5 tests from the command line. Unfortunately, I have some outside dependencies that prevent me from using Maven, Gradle, or other build systems.

在 JUnit 4 中,我可以做到这一点

In JUnit 4, I could accomplish this like

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

是否有 JUnit 5 的等效项?我只想知道测试是否像在 IntelliJ 中运行时一样成功.

Is there an equivalent for JUnit 5? I'd simply like to know if test succeeded similar to when it runs in 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.

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

The ConsoleLauncher is a command-line Java application that lets you launch the JUnit Platform from the console. For example, it can be used to run JUnit Vintage and JUnit Jupiter tests and print test execution results to the console.

一个可执行的 *junit-platform-console-standalone-.jar* 与所有包含的依赖项发布在中央 Maven 存储库中在 junit-platform-console-standalone 下目录.你可以运行如下图所示的独立 ConsoleLauncher.

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. You can run the standalone ConsoleLauncher as shown below.

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

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

For details about the options consult https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher please.

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

Tailored to your example and using JUnit Platform version 1.3.1, the commands could look like those:

$ 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
...

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgjgjc
系列文章
更多 icon
同类精品
更多 icon
继续加载