'Running Spring boot Tests from with Vim

I have a Mac and iTerm2 installed, I used the command vi *file name*.java to open and edit the file. This file has unit tests annotated with the @Test keyword in Spring.

How do I run these tests from the command line instead of clicking on the Test annotation in IntelliJ?



Solution 1:[1]

If using maven: To run all test cases, go into the project's root and run (if running from somewhere else, use the path)

mvn test

To run all tests from a single class:

mvn test -Dtest=<YourTestClass>

To run a test case from a test class:

mvn test -Dtest=<name-of-your-test-class>#<name-of-your-test-method> test

If you are not using maven (or Gradle):
see: How to launch JUnit 5 (Platform) from the command line (without Maven/Gradle)?

EDIT: Looks like the OP is unable to run these commands in his/her setup. Make sure you have correct dependencies and plugins (Junit and SureFire) in your pom. E.g. I created a demo maven project with this pom (only including the Junit and Surefire).

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M6</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

Here are the screenshots of working mvn test commands.

enter image description here

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1