0

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?

  • 2
    Welcome to the Java world. There is a good deal to learn and, specific to your question, start with the [docs](https://spring.io/guides/gs/testing-web/). There are many options - post back here if you get stuck. – stdunbar May 19 '22 at 00:09
  • You have 2 options - with maven or without maven (Assuming the original project is using maven and not gradle) with maven: `mvn test -Dtest= path.to.package.` without maven: https://stackoverflow.com/questions/52373469/how-to-launch-junit-5-platform-from-the-command-line-without-maven-gradle/52373592#52373592 – Tintin May 19 '22 at 00:28
  • @Tintin this runs all the tests I believe. Is there a way to run one specific test? – William Johnson May 19 '22 at 20:30

1 Answers1

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

Tintin
  • 2,853
  • 6
  • 42
  • 74
  • When I try this, I get the error ```The goal you specified requires a project to execute but there is no POM in this directory``` even tho there are multiple Pom files in this entire component – William Johnson May 20 '22 at 18:32
  • The error is self explanatory. As I said in my answer above - you need to run this command in your projects root (thats where the pom.xml would be). – Tintin May 20 '22 at 19:03
  • Your method still does not work, please revise – William Johnson May 23 '22 at 23:54
  • Well it does if you have a maven project. I suspect, you don't. – Tintin May 23 '22 at 23:56
  • I do have a maven project, I don't see why you are being so rude about this. – William Johnson May 24 '22 at 23:42
  • @WilliamJohnson I was trying to help. I added some more information that might help you (e.g. pom dependencies). Also I have attached the screenshot of these working commands in the answer. If this doesn't work for you, I am not sure what else conclusion to draw other than that your project is probably not set up correctly. – Tintin May 25 '22 at 18:53