Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Unfortunately, I have some outside dependencies that prevent me from using Maven, Gradle, or other build systems.

In JUnit 4, I could accomplish this like

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

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());
    }
}
like image 538
Rachel H Avatar asked Sep 17 '18 18:09

Rachel H


People also ask

Can I use JUnit without maven?

Yes, you need to download the junit5 JARs as mentioned in https://junit.org/junit5/docs/current/user-guide/#overview .

How do I run a JUnit 5 test in Maven?

The Maven Surefire Plugin 2.22. 0 (or newer) provides native support for JUnit 5. If we want to use the native JUnit 5 support of the Maven Surefire Plugin, we must ensure that at least one test engine implementation is found from the classpath. We can run our unit tests by using the command: mvn clean test.

What is JUnit platform launcher?

platform. launcher. Public API for configuring and launching test plans. This API is typically used by IDEs and build tools.


1 Answers

Sure, use the ConsoleLauncher.

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.

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>

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

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
...
like image 116
Sormuras Avatar answered Sep 27 '22 23:09

Sormuras