40

In a Maven project, I have test classes and source classes in the same package, but in different physical locations.

.../src/main/java/package/** <-- application code
.../src/test/java/package/** <-- test code

It's no problem to access the source classes in the test classes, but I would like to run a test runner in the main method and access the AllTest.class so that I can create jar and execute my tests.

 public static void main(String[] args) {
    // AllTest not found
    Result result = JUnitCore.runClasses(AllTest.class);
    for (Failure failure : result.getFailures()) {
        System.out.println(failure.toString());
    }
    System.out.println(result.wasSuccessful());
}

But it doesn't work as I don't have access to the test code. I don't understand since they are in the same package.

Question: how can access test classes from application classes? Alternatively, how can Maven package a fat jar including test classes and execute tests?

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
jam
  • 1,253
  • 1
  • 12
  • 26
  • 1
    they are in the same package but in different scopes, good practices are enforced by Maven on applying the compile scope (main/java/..) visibility in your tests, but not the other way around – A_Di-Matteo Mar 16 '16 at 21:52
  • What? Are you trying to write a class in `src/main/java` that depends on a test class in `src/test/java`? If so, you're doing something wrong. What is the real problem you want to solve? – Tunaki Mar 16 '16 at 21:52
  • I have to create a binary which executes all tests and prompt the output to terminal in main – jam Mar 16 '16 at 21:56
  • But you can have the same main in the test scope then and package it as additional project artifact – A_Di-Matteo Mar 16 '16 at 21:58
  • how would I achieve that in my pom.xml ? – jam Mar 16 '16 at 21:59
  • Possible duplicate of [How to include test classes in Jar created by maven-shade-plugin?](https://stackoverflow.com/questions/10307652/how-to-include-test-classes-in-jar-created-by-maven-shade-plugin) – gcandal Aug 17 '18 at 13:53

1 Answers1

66

You should not access test classes from your application code, but rather create a main (the same main) in the test scope and create an additional artifact for your project.

However, in this additional artifact (jar) you would need to have:

  • The test classes
  • The application code classes
  • External dependencies required by application code (in compile scope)
  • External dependencies required by the test code (in test scope)

Which basically means a fat jar with the addition of test classes (and their dependencies). The Maven Jar Plugin and its test-jar goal would not suit this need. The Maven Shade Plugin and its shadeTestJar option would not help neither.

So, how to create in Maven a fat jar with test classes and external dependencies?

The Maven Assembly Plugin is a perfect candidate in this case.

Here is a minimal POM sample:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample</groupId>
    <artifactId>sample-project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>com.sample.TestMain</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

The configuration above is setting the main class defined by you in your test classes. But that's not enough.

You also need to create a descriptor file, in the src\main\assembly folder an assembly.xml file with the following content:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>fat-tests</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>test</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/test-classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*.class</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
    </fileSets>
</assembly>

The configuration above is:

  • setting external dependencies to be taken from the test scope (which will also take the compile scope as well)
  • setting a fileset to include compiled test classes as part of the packaged fat jar
  • setting a final jar with fat-tests classifier (hence your final file will be something like sampleproject-1.0-SNAPSHOT-fat-tests.jar).

You can then invoke the main as following (from the target folder):

java -jar sampleproject-1.0-SNAPSHOT-fat-tests.jar

From such a main, you could also invoke all of your test cases as following:

  • Create a JUnit test suite
  • Add to the test suite the concerned tests
  • Invoke the test suite from your plain Java main

Example of test suite:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ AppTest.class })
public class AllTests {

}

Note: in this case the test suite is only concerning the AppTest sample test.

Then you could have a main class as following:

import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;

public class MainAppTest {

    public static void main(String[] args) {
        System.out.println("Running tests!");
        
        JUnitCore engine = new JUnitCore();
        engine.addListener(new TextListener(System.out)); // required to print reports
        engine.run(AllTests.class);
    }
}

The main above would then execute the test suite which will in chain execute all of the configured tests.

Saikat
  • 14,222
  • 20
  • 104
  • 125
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • 1
    and where to specify the manifest attribute for command line call java -jar test-final.jar – jam Mar 16 '16 at 22:08
  • I actually create a final.jar a final-test.jar and final-source.jar and my intention is to execute the final.jar where the compiled sources are therefore I wanted to access the all test class. – jam Mar 16 '16 at 22:15
  • so as you mentioned it doesn't add junit which I need for the test runner, how to add this dependency to the test-jar ? – jam Mar 16 '16 at 22:32
  • I found this one (http://stackoverflow.com/questions/4648341/how-to-export-junit-test-suite-as-executable-jar) – jam Mar 16 '16 at 22:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106517/discussion-between-jam-and-a-di-matteo). – jam Mar 16 '16 at 23:03
  • @A_Di-Matteo How to import class `AllTests.class` into `MainAppTest` class because it is NOT present in `src/main` but `src/test`? – mCs Apr 05 '17 at 08:43
  • @mCs you should actually add `AllTests` class into `src/test` because it belongs to test scope anyway, from there you can add classes from `src/main` AND `src/test`, that's the whole point. – A_Di-Matteo Apr 05 '17 at 10:12
  • @A_Di-Matteo I did add it to `src/test` and Intellij keep saying it cannot resolve it. I have also tried to run it from the console but I keep getting this error: http://stackoverflow.com/questions/43228243/run-junit-test-from-within-jar-cannot-find-the-test-class however without the `main` class part. – mCs Apr 05 '17 at 10:19
  • Is it possible to do this for a multi module project? I want to have all my test .class files of all modules in one jar with their dependencies. – ParSal Aug 09 '23 at 08:36