Maven Unit Tests - java

I am working on a project written using Maven.
If I do
mvn clean install
then I expect that it will run the all unit tests under /test forder.
I redirected the build process and stored into an output file and tried to find out unit tests by the name. However, I wasn't able to find some of the unit tests. It seems like some of the unit tests were actually not executed during the build process.
My question is how do I know the selection of unit tests that will be executed during the build process.

You may try explicitly naming the group(s) in the command line like this:
mvn clean install -Dtest.groups="unit,integration"
or simply
mvn test -Dtest.groups="unit,integration"
This is an example of what I use daily. My tests are either in the unit or the integration group. If you wish to run all (or some) tests without the need for explicitly specifying which gorups to use you have to modify your maven configuration though.

The unit tests in Maven will be selected by their name which should follow the naming convention
<includes>
<include>**/*Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
Furthermore you need to put the tests to the default location src/test/java/.
You can control running only a single test by using:
mvn -Dtest=MyTest test
Running integration test is the job of the maven-failsafe-plugin which is handled in the integration-test phase. The naming convention for integration tests is:
<includes>
<include>**/*IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
To get integration tests running you need to add a configuration part for the maven-failsafe-plugin.

Related

Jmeter maven plugin skip unit tests

I am trying to run jmeter tests with jmeter maven plugin as shown in https://github.com/jmeter-maven-plugin/jmeter-maven-plugin/wiki/Configuring-the-jvm-that-the-jmeter-process-runs-in
When I run mvn verify it runs the jmeter test but also all the unit tests under src/test/. Is there a way that I can skip unit tests and only run the jmeter test?
Thanks in advance!
The easiest is just running single jmeter goal like:
mvn jmeter:jmeter
this way you will run only performance tests, all other build procedures will be skipped.
A harder but more flexible way would be using Maven Builde Profiles, see Maven – How to skip unit test
More information: How to Use the JMeter Maven Plugin

How to run maven test with mysql running in docker (using maven-surefire-plugin)

I found several post that explains how to run integration tests wising maven and docker. They basically explains hoy to use/setup fabric8 maven-docker-plugin and maven failsafe plugin.
I'm wondering if is possible to use fabric8, for example, with surefire. My specific need is: I need to run my tests (JUnit tests) but first I need/want to start a docker image with MySQL running on it.
I'm not tied to fabric8. If there is another way to start the docker image before start my tests and stop/kill it once tests run finish, that will help me a lot.
Thanks in advance.
Maxi
Yes you can definitely do that. The idea is to use a maven docker plugin such as fabric8 docker plugin and start a container before the test phases and stop it after the test phase.
But note that technically tests that connect to databases are not unit tests, they should be integration tests.
There are many tutorials online to do that such as INTEGRATION TESTING WITH DOCKER AND MAVEN
You can adapt this for unit tests by just changing the phases when the docker plugin runs.
You can change <phase>pre-integration-test</phase> to a phase that starts before the maven test phase such as <phase>generate-test-resources</phase> and stop the container once the test ends for example:
<phase>prepare-package</phase>.
Note that there are no nice phase names as there are for integration test, as it is not ideal to start external services when running unit tests. But anyway it works.
For a complete reference on maven phases check Introduction to the Build Lifecycle

Is there any class name conventions for pre-integration-test and post-integration-test similar to integration-test

For junit test cases The class should be named like XXTest.java
For intergation-test the class should be named like XXIT.java
Is there any class name conventions for pre-integration-test and post-integration-test goals in maven similar to unit and integration test?
The convention is that no tests of any kind are run during pre- and post-integration-test.
As suggested by #biziclop, the way to think about this is "pre-X" and "post-X" - these are things executed before/after "X". So, pre-integration-test phase is not the phase where "pre-integration" tests are run, but rather it is the phase before "integration-test" is run.
These goals are usually used for managing system state for the running of integration tests. For example, pre-integration-test may start a container that is used by the integration tests, while post-integration-test will stop the container.
There is nothing wrong with using a variety of plugins and/or custom code to support the execution of your integration tests. If using exec-maven-plugin really bothers you, you can always create your own maven plugin that knows about whatever naming convention you want to use to find java classes in your build and execute them. Then each of your modules can use the generic plugin to invoke the specific set of classes to do the setup/tear-down.

Managing TestNg suite from Jenkins parameterized build

I'm running my tests in Jenkins and Maven and have different test suites in several TestNG.xml files.
Now I manage them directly in pom in this way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>Testing_Fuzzy_Logic.xml</suiteXmlFile>
<suiteXmlFile>Testing_ACL.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
But I'd like to be able to select a specific test suite when start a Jenkins build. And I'd like all the suites to be included to the build as default if I did not select a specific one. So my question is:
What is the proper way to write all my TestNG.xml files in Jenkins parameters and send them to pom file to ask Maven use them when running tests?
Something like this:
Jenkins variable:
Set it to pom file somehow:
The issue has been solved by an example above. At first there was an error in file name and it confused me. I fixed it and my example works okay now.
So one of solutions is: a string TEST_NG_XML parameter which is added to parameterized Jenkins build. All test suites are set there and separated by comma by default. When needed, any of them can be removed (See examples above).
But if there are any other solutions, I'll be delighted to see them.
On jenkins, you can pass top level maven goals in following format
test -DargLine="-DTest_NG_XML=src/test/resources/testng.xml"
In this way , you are passing the value of your Test_NG_XML variable while running the maven test phase.In general execute following maven goal on jenkins.
-DargLine="-Dparameter=value"
test -DsuiteXmlFile=src/test/resources/testng1.xml,src/test/resources/testng2.xml
How can one be sure the precedence of execution of test suites passed this way?
I was facing the same issue, I have split it into separate Jenkins builds with each passing separate suite, to be absolutely sure certain suite runs before the other.
I would like to simplify it, possibly passing this way as indicated above... but did you try the order of execution? Thanks

cucumber tests on docker won't fire at the integration-test phase

I'm having some problems getting my cucumber/phantomJs test to run against the docker container.
My plan is to start the tomcat docker container at the maven pre-integration-test phase. Then at the integration-test phase i want to run the cucumber tests. For this i have disabled the surefire plugin to run test phase and included the failsafe plugin. Though some how the integration-test phase does nothing. I have no idea whats wrong with it.
The docker containers work fine and are started at the pre-integration-test phase. They also stop at the post-integration-test phase. The war is deployed and runs like it should run. No problems there.
The cucumber tests run at the test phase when the surefire plugin is enabled for test. In the sample project you can change this by editing the pom at the section for the surefire plugin with to
<configuration>
<skip>true</skip>
</configuration>
I have created a sample project at https://github.com/abroer/cucumber-integration-test.git
The project can be run using mvn clean verify
Any suggestions on how to fire the cucumber tests at maven integration-test phase are appriciated.
Your cucumber test runner is called RunCukesTest. This pattern is not included by default by the failsafe plugin, see https://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#includes . You have either to rename your class to say RunCukesIt or adjust the configuration of the failsafe plugin.

Categories

Resources