1

How can I execute SampleFlowTest.java via maven?

The code inside SampleFlowTest.java is:

package com.myname.module.test;

import com.myname.partA.something;
import java.util.Scanner;
import java.util.ArrayList;

public class SampleFlowTest {
    public static void main(String[] args) {
        // something I want to execute
    }
}

The files tree is as below.

.
├── pom.xml
├── resource
│   ├── fileA.txt
│   └── fileB.txt
├── src
│   ├── main
│   │   └── java
│   │       └── com
│   │           └── myname
│   │               ├── partA
│   │               │   └── Aaa.java
│   │               ├── partB
│   │               │   ├── B.java
│   │               │   ├── Bb.java
│   │               │   └── Bbb.java
│   │               └── module
│   │                   ├── M1.java
│   │                   └── M2.java
│   └── test
│       └── java
│           └── com
│               └── myname
│                   ├── partA
│                   │   └── test
│                   │       └── AaaTest.java
│                   ├── partC
│                   │   └── service
│                   │       └── test
│                   │           ├── BTest.java
│                   │           └── BbbTest.java
│                   └── module
│                       └── test
│                           └── SampleFlowTest.java
└── target
    ├── classes
    │   ├── com
    │   │   └── myname
    │   │       ├── partA
    │   │       │   └── Aaa.class
    │   │       ├── partB
    │   │       │   ├── B.class
    │   │       │   ├── Bb.class
    │   │       │   └── Bbb.class
    │   │       └── module
    │   │           ├── M1.class
    │   │           └── M2.class
    │   └── somefile.txt
    └── test-classes
        ├── com
        │   └── myname
        │       ├── partA
        │       │   └── test
        │       │       └── AaaTest.class
        │       ├── partC
        │       │   └── service
        │       │       └── test
        │       │           ├── BTest.class
        │       │           └── BbbTest.class
        │       └── module
        │           └── test
        │               └── SampleFlowTest.class
        └── module.properties

I have tried like Maven: How to run a .java file from command line passing arguments said but it still doesn't work.

I executed mvn exec:java -Dexec.mainClass=com.myname.module.test.SampleFlowTest, and error occurred , part of the result is:

[WARNING] 
java.lang.ClassNotFoundException: com.myname.module.test.SampleFlowTest
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:281)
    at java.lang.Thread.run(Thread.java:745)

What command should I execute other than mvn exec:java -Dexec.mainClass=com.myname.module.test.SampleFlowTest?

AnnieFromTaiwan
  • 3,845
  • 3
  • 22
  • 38
  • I think `mvn exec` does not look into your test class path. Why has your test class a `main` method? Do you want to run your unit tests? – schrieveslaach Aug 15 '16 at 09:31

3 Answers3

4

SampleFlowTest.java should be in the src/main/java tree.

  • src/test/java is for unit and integration test classes.

  • src/main/java is for classes used your application.

SampleFlowTest is not a test class since it contains a static void main(String[] args) method. A test class should contain assertions but your class doesn't contain them. On the contrary, it contains a mechanism used for launching an application : static void main(String[] args)

mvn exec:java

is for running programs. So, by default, i guess it searches the execution class exec.mainClass in src/main/java.

If you don't want to change your layout, you can configure the exec:javagoal for using another scope that the source scope. You can do it by setting the property exec.classpathScope when you run your command in this way :

 mvn exec:java -Dexec.mainClass=com.myname.module.test.SampleFlowTest -Dexec.classpathScope="test"
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Please stop copying from my answer! Without compiling test via `mvn test` or `test-compile`, your command will not work. – Rocherlee Aug 15 '16 at 09:59
  • I copied nothing since I don't refer your commands. You have told yourself. So, please, calm down. I edited my answer for bringing precisions. They have no relation with your response. – davidxxx Aug 15 '16 at 10:05
  • You are right for `mvn test` according if it is needed to compile or not the test class. If you work with an IDE, it should be useless. Contrary to you, I explained the cause of the problem. In the first part, I discuss about its code layout which is not very conventional. After, I discuss about how bypassing this. I edited for the bypassing but it has no relation with your post. It takes time, sorry but it helps people to understand the problem and it is more helpful that proposing how to bypass without giving explanations. – davidxxx Aug 15 '16 at 10:27
  • You and I posted an answer at the same time. Then 9 min later you added the bypassing part which also **included** my answer. Initially you did not have the idea to use `-Dexec.classpathScope="test"`. From what I saw, your initial solution was to move main() to `/src/main/java`. Yes, I only provided the fast solution since he/she only asked for a working command. Anyway, I appreciate the detailed answer you give here. – Rocherlee Aug 15 '16 at 10:58
  • I didn't want to post. I have posted because you have posted. Else it may give the impression I copied. Since, I didn't finish my answer, then I edited for terminating it. That's all. So, please if you want to play to the game : fast, fast, fast, you can. But don't insult and accuse people which will do complete answers. It is not respectful. It is my last answer. We will not discuss about it 5 hours. – davidxxx Aug 15 '16 at 11:27
3

It's because exec:java does not compile your test in /src/test

You need to run the goal test-compile before exec:java

mvn test-compile exec:java -Dexec.mainClass=com.myname.module.test.SampleFlowTest -Dexec.classpathScope=test
Rocherlee
  • 2,536
  • 1
  • 20
  • 27
  • `exec:java` compiles nothing. It is a goal for running program classes. It run program classes by looking by default in Maven sourceDirectory of the project which is by default `src/main/java` and not `src/test/java` – davidxxx Aug 15 '16 at 10:19
  • what I meant was `exec:java` goal does not map to any default maven phase for compiling test. So running it only will not compile the test in `/src/test` – Rocherlee Aug 15 '16 at 10:44
0

In your test class use annotation @Test above method you want to run. This annotation comes from "org.junit.Test". Then go to terminal, go to the project path. Like if your maven project name is "SampleApp" and its at path "X". Then go to "/X/SampleApp" in terminal. Then execute command "mvn clean install", it will run all test cases you have written in "/src/test..".

For running single test or test class refer to this link.

Ashutosh Jha
  • 1,465
  • 1
  • 17
  • 28