1

I have a project structure:

├── main
│   ├── java
│   │   └── qbs
│   │       ├── QbsApplication.java
│   └── resources
│       ├── application.properties
|
└── test
    └── java
        └── qbs
            └── QbsApplicationTests.java

I have build it with mvn clean:install and created jar file. Now I want to run the QbsApplicationTests using the command line. To do it I have put two jars into one dir:

-rw-rw-r--. 1 a a 29M 04-05 10:30 fi.qbs-0.0.1-SNAPSHOT.jar
-rw-rw-r--. 1 a a  1M 2014-12-04  junit-4.12.jar

and executed the following command:

java -cp .:fi.qbs-0.0.1-SNAPSHOT.jar:junit-4.12.jar org.junit.runner.JUnitCore qbs.QbsApplication

However, I keep getting the following error

JUnit version 4.12
.E
Time: 0,003
There was 1 failure:
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [qbs.QbsApplication]
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
    at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
    at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: qbs.QbsApplication
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.junit.internal.Classes.getClass(Classes.java:16)
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
    ... 4 more

FAILURES!!!
Tests run: 1,  Failures: 1

Question:

  • How should I run the QbsApplicationTests tests form console?

EDIT I have also tried to add the following:

@SpringBootApplication
public class QbsApplication {

    public static void main(String[] args) {

        SpringApplication.run(QbsApplication.class, args);
        System.out.println("Running tests!");

        JUnitCore engine = new JUnitCore();
        engine.addListener(new TextListener(System.out) ); // required to print reports
        engine.run( QbsApplicationTests.class);
    }

}

to the main class, but Intellij keeps saying that the QbsApplicationTests cannot be resolved.

mCs
  • 2,591
  • 6
  • 39
  • 66

1 Answers1

0

The reason you split up your source and test classes in the tree structure like that is because when shipping your final jar, your clients are not interested in the test classes. They only care about the actual code you have written.

That is why building the jar with maven will automatically exclude your test directory.

However, you are right that you want to test the correctness of your code. But you want to do this before you build the jar for shipping.

Instead run mvn test like suggested here

Or since you're using IntelliJ, just right click on the test class and click "run".

Imus
  • 802
  • 6
  • 11
  • I need to run the tests from the project on a remote server. I where I only have java installed. This is why I need to run tests from jar with numerous dependencies- or is there other way? – mCs Apr 05 '17 at 11:48
  • @mCs What Kind of tests you have to run? – Jens Apr 05 '17 at 12:13
  • @Jens The tests generate data at the server database. – mCs Apr 06 '17 at 06:22