Maven Eclipse unit test configuration - maven-2

I use Eclipse and for some of my unit tests, I need to set some JVM args for the test to work, -Djava.library.path in particular. I set it in my POM file as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Djava.library.path=target/dll</argLine>
</configuration>
</plugin>
and it works correctly when I build with Maven. However, when I run in Eclipse (even with the Maven plug-in installed - I have everything else in my Eclipse environment working properly with maven), this JVM argument is not applied. Does anyone know why this is or how to fix this?

Running the test in Eclipse with the JUnit test runner does not invoke maven , even if you are using m2eclipse.
I suggest you either:
create a maven launch configuration for the test goal;
add -Djava.library.path=target/dll to your JVM arguments in the JUnit launch configuration.

Related

Why is Surefire not executing my JUnit5 tests?

I'm trying to convert one Maven SpringBoot (2.3.12) app from JUnit4 to JUnit5. I've read many different posts on how to do this.
I'm able to execute my JUnit5 tests in Eclipse.
My problem is that I am unable to get Maven Surefire to execute my JUnit5 tests. I've tried various configuration variations. When it gets to the Surefire step, it ONLY executes my old JUnit4 tests and simply ignores any JUnit5 tests. I've verified that if one of those JUni4 tests is executed from this, and I convert that test to JUnit5, it will then ignore that test. At this point, I'm only focusing on the tests that were using the Mockito runner, as they are simpler to convert than the Spring runner tests (the vast majority of which likely could be changed to just use the mockito runner).
This is what I have in the surefire plugin config:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- <groups>UnitTest</groups> -->
<reuseForks>false</reuseForks>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<argLine>${surefireArgLine}</argLine>
<skipTests>${skip.unit.tests}</skipTests>
<excludes>
<exclude>**/component/*.java</exclude>
<exclude>**/contract/*.java</exclude>
<exclude>**/integration/*.java</exclude>
</excludes>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
</dependency>
</dependencies>
</configuration>
</plugin>
I currently have "groups" commented out until I understand what it is doing without that.
This is an excerpt of one of the unit tests:
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
#ExtendWith(MockitoExtension.class)
public class BDSHelperTest {
Before I converted this to use the JUnit5 classes, it would execute when I run "mvn package". Now that I've converted it, Surefire is not executing it.
This is what I see at the start of the surefire step:
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) # NotesMs ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
At this point, it shows the test runs for all the old JUnit4 tests, but it doesn't run any of the JUnit5 tests.
I am the Apache developer of Surefire. I have created a project which proves that it works with Surefire 3.0.0-M5, JUnit Jupiter 5.8.2 and Mockito Extension.
Please do not use JUnit4 and Vintage in this case.
It is not necessary to use a dependency inside of the plugin.
Use a dependency in the project POM.
Follow it on Github:
https://github.com/Tibor17/junit5-mockito-examples

How do I generate surefire report for all tests and make this work with jenkins?

I have a large number of Junit tests running selenium on eclipse, what I would like to do is generate a Junit or surefire report for the tests after all of them are run. I know that with maven you can do this for individual tests by running mvn test on the console and generating a report with:
mvn surefire-report:report-only
However, this tests and produces a report for each individual test, is there a way to make this work for multiple tests? The reason behind this is because I run these tests headlessly on Jenkins, I know that jenkins email plugin can allow me to pass a html report post build which would give me an idea of test success and failure.
You can try using this command:
#mvn clean test
This will clean the existing reports and run all test cases in one go and generate surefire-reports for all.
Please let me know if this what you are looking for. :)
Firstly you need to have all the right plugins and dependencies in your pom.xml file.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
<!-- `mvn clean test site` to generate the junit html report-->
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</reporting>
Following this add the maven command to your maven build as shown.
After you run this your IDE will run all the tests in that maven build and generate a surefire.html report which can be found in target/site/surefire-report.html
(You may need to update your maven project to see the folder come up for the first time by right clicking the project, then hover over maven and then click update maven project.)
I would advise doing this as it then works with jenkins when it runs your tests in headless mode which can then be attached to your email extension plugin.

Unable to Deploy to Tomcat using Maven3 with Mail API

I am working on an application where we need to use java mail functionality. We have started using maven 3.x as out build tool.
Everything was working fine till Java Mail API has not been introduced. We are using Eclipse with M2Eclipse plugin but most of our deployment work is being done by maven Command line.
We have introduced following dependency in our pom.xml and I have verify that both mail.jar and activation.jar are in there respected folder structure.
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
We tried the following command
> mvn clean
and then
>mvn tomcat:deploy
Though maven is showing that it has successfully deployed war on the tomcat but tomcat console showing that it is failing to deploy application and in other successful cases we are facing a strange issue, as we are using hibernate for persistance layer so on examing the folder structure it came out that the mapping file .hbm files are missing due to which Session factory is not creating and server is not able to startup.
Here is the snap shot of plugin entry
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<warFile>${project.build.directory}/${project.build.finalName}.war</warFile>
<url>http://localhost:8080/manager/html</url>
<server>localhost</server>
<path>/blood_donor</path>
</configuration>
</plugin>
From you procedure I see:
mvn clean - that deletes target directory with your build
mvn tomcat:deploy - should take the build (which was deleted with mvn clean) and deploy it on tomcat
There is no build phase. So use instead mvn clean package tomcat:deploy. If your application is already deployed in tomcat try mvn clean package tomcat:redeploy. For details check plugin documentation.
Which Tomcat version do you use?
According to http://repo1.maven.org/maven2/org/codehaus/mojo/tomcat-maven-plugin/ you use the version 1.1. In the plugin documentation you can find there that Tomcat 7 is not supported in this version. For that you must upgrade to version 2.0. See http://tomcat.apache.org/maven-plugin-2.0-SNAPSHOT/
Probably yo

maven-assembly-plugin causing tests to run twice

I have a maven project where I am using the assembly plugin.
I typically create my artifacts by running:
mvn clean verify assembly:assembly
(I have integration tests which I want run separately to unit tests).
When this runs, the assembly plugin is running the unit tests itself.
This causes them to be run twice.
Is there a way I can tell the assembly plugin not to run the tests?
I am tempted to run this in two steps:
1. mvn clean verify
2. if previous command successful, run mvn assembly:assembly -DskipTests=true
However, this is a little clumsy and would rather the single command.
Thanks,
Steven
When this runs, the assembly plugin is running the unit tests itself. This causes them to be run twice.
The assembly:assembly goal Invokes the execution of the lifecycle phase package prior to executing itself and running it on the command line will thus invoke any phase prior to package. And this includes the test phase.
Is there a way I can tell the assembly plugin not to run the tests?
No. My suggestion would be to create the assembly as part of the build lifecycle instead of invoking the plugin on the command line i.e. to bind it on a particular phase. For example:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<executions>
<execution>
<id>create-my-assembly</id>
<phase>package</phase><!-- change this if not appropriate -->
<goals>
<goal>single</goal>
</goals>
<configuration>
...
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And if you don't want the assembly to be created if your integration tests fail, then bind it on a later phase (e.g. post-integration-test or verify).
And if you don't want the assembly to be systematically created, put the above configuration in a profile.

Maven - Skip building test classes

Is there a simple way to not build the test classes?
mvn clean install -Dmaven.test.skip=true
According to the documentation on the Maven Surefire Plugin, -Dmaven.test.skip should skip both compilation and execution of the tests. By contrast, -DskipTests just skips the test execution: the tests are still compiled.
Just to be explicitly clear:
skipTests will compile anything in the <testSourceDirectory>, but will not execute them.
maven.test.skip will NOT compile any tests, but WILL execute any compiled tests that made their way into the <testOutputDirectory>.
So the behavior of the above 2 is opposite. Just wanted to point out that maven.test.skip doesn't skip compilation AND execution if test files are unpacked/copied/etc. into <testOutputDirectory>.
Also, depending on which version of Maven your using, there's also maven.test.skip.exec=true which additionally skips test execution similar to skipTests.
Run a phase that doesn't include test-compile, for example compile.
mvn clean compile
I'm not an expert at maven, but what I use in my current project is:
mvn clean install -DskipTests=true
Depending on your use case using:
mvn compile -DskipTests=true
might work for you too.
I found a work-around in another question, which actually overrides a plugin execution by adding the following snippet in your pom.xml by default:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
This seems to work but definitely does not disable phase but disables the default actions that a plugin defines at a specific phase.