Assumptions
We are dealing with a “stand-alone” test, meaning we don’t have a main source code to test in the same project. Instead, the test source code contains all that is required to execute the test. A common scenario is a UI functional testing of a web app using Selenium, where we just need access to the app through a URL, and all the page classes and tests are contained in a single source of code.
Also, this implementation assumes maven keeps the default directory structure:
src/main/java
src/main/resources
src/test/java
src/test/resources
There are two approaches that can be used:
A. Test code is in “main”
- The test code and resources are contained in (or moved to) the main module
src/main/java and src/main/resources
- Create a
Main class where you can start the execution from. Example:
public class TestMainRunner {
public static void main(String[] args) {
String[] cucumberArgs = new String[] {
"-g", "steps",
"classpath:features",
"-p", "pretty"
};
Main.run(cucumberArgs);
}
}
- Add
maven-assembly-plugin to pom.xml to enable creating a .jar file including the project dependencies and also to specify the main class to be executed when running the .jar:
<build>
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package
<goals>
<goal>single
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>run.TestMainRunner</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
...
</build>
</plugins>
To generate the .jar file run from terminal:
mvn clean compile assembly
Note: Single jar file is output to target folder
- Define
src/main/java and src/main/resources as test source and resources in the pom.xml:
<build>
...
<testSourceDirectory>
src/main/java
</testSourceDirectory>
<testResources>
<testResource>
<directory>src/main/resources</directory>
</testResource>
</testResources>
...
</build>
By doing this, test can be executed by running from the terminal:
mvn test
Note: When executing mvn test, test-classes folder is generated in target with same content as classes. That might cause some warning during execution, nothing to worry about.
B. Test code is in “test”
In case tests are developed under test module and there is not option to move them to main, this could one helpful approach.
- The test code and resources are contained in (or moved to) the test module
src/test/java and src/test/resources
- Create a
Main class where you can start the execution from. Example:
public class TestMainRunner {
public static void main(String[] args) {
String[] cucumberArgs = new String[] {
"-g","steps",
"classpath:features",
"-p", "pretty"
};
Main.run(cucumberArgs);
}
}
- Add
maven-assembly-plugin to pom.xml to enable creating a .jar file including the project dependencies and also to specify the main class to be executed when running the .jar:
<build>
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>run.TestMainRunner</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
...
</build>
</plugins>
To generate the .jar file run from terminal:
mvn clean compile assembly:single
jar file is output to target folder
mvn test can be executed without any modification to the pom.xml file, since tests are contained in the default directory defined by maven.
- A last tweak required to execute the tests from the IDE (▶️), is to specify the resources directory in the
pom.xml:
<build>
...
<!-- Required to execute from IDE when running feature directly -->
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
</resources>
...
</build>