How to implement Integration test using not current Spring 4.0.7 RELEASE version - spring

Welcome,
We are trying to add integration tests to our Spring project.
Here are listed current Maven dependencies.
-Spring Version: **4.0.7 RELEASE**
-JUnit: **4.8**
Unit tests already exist. They are executed with: #RunWith(SpringJUnit4ClassRunner.class)
We tried to use Maven configuration to exclude IntegrationTests while executing unit tests.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</plugin>
Unfortunately it worked only partialy, beacuse now integration tests are not executed at all. I would be grateful if anyone could help use solving this issue.
Thank You in advance!

You need to add goal into your failsafe configuration. Please see below: http://maven.apache.org/components/surefire/maven-failsafe-plugin/usage.html

Related

Maven + Surefire: Overwrite temp and output directory

I am trying to overwrite temporary and output directory for surefire plugin. I have following plugin definition:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<tempDir>c:/tmp/a-tests-temp</tempDir>
<outputDirectory>c:/tmp/a-tests-out</outputDirectory>
</configuration>
</plugin>
It does not work. It continue using /target/surefire and target/surefire-reports.
The idea is to split different concurrency maven test runs over the same project directory for quick tests on the developer workstation.
Sorry for late reply, ive only managed to use relative path location to change the path for the temp folder
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<testClassesDirectory>${project.buildDirectory}/test-classes</testClassesDirectory>
<reportsDirectory>${project.buildDirectory}/surefire-reports</reportsDirectory>
<classesDirectory>${project.buildDirectory}/test-classes</classesDirectory>
<tempDir>../${project.buildDirectory}/surefire</tempDir>
<useFile>false</useFile>
<includes>
<include>**/*Spec.*</include>
</includes>
<systemPropertyVariables>
<geb.env>${geb.env}</geb.env>
<geb.build.reportsDir>${project.buildDirectory}/${geb.build.reportsDir}</geb.build.reportsDir>
<com.athaydes.spockframework.report.outputDir>${project.buildDirectory}/spock-reports</com.athaydes.spockframework.report.outputDir>
<com.athaydes.spockframework.report.aggregatedJsonReportDir>${project.buildDirectory}/spock-reports</com.athaydes.spockframework.report.aggregatedJsonReportDir>
</systemPropertyVariables>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>

Why Spring Boot parent starter modifies surefire defaults?

I'm migrating existing application to Spring Boot. To simplify project configuration I decided to setup spring-boot-starter-parent as parent.
The problem is that surefire configuration
<configuration>
<includes>
<include>**/*Tests.java</include>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
</configuration>
is different than default surefire settings and broke my build.
How can I "undo" includes/excludes from Spring Boot? Not overwrite, but go back to surefire defaults.
I think that spring-boot-starter-parent should be surefire-neutral.
PS. Sooner or later I will solve my problem myself. Current issue is rather question to Spring Boot
I restored surefire defaults for includes and excludes with
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
<excludes>
<exclude>**/*$*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

maven-surefire-plugin doesn't run parallel tests

I have a project Serenity+Java+JUnit and I try to run my tests in parallel.
I paste this into my pom and after mvn integration-test, it is still run in a chain :(
What I have done wrong?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<parallel>all</parallel>
<forkMode>perthread</forkMode>
<threadCount>4</threadCount>
</configuration>
</plugin>
Have resolved it by adding another plugin instead of surefire-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<forkCount>5</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
<systemPropertyVariables>
<databaseSchema>MY_TEST_SCHEMA_5</databaseSchema>
</systemPropertyVariables>
<workingDirectory>FORK_DIRECTORY_5</workingDirectory>
</configuration>
</plugin>
Not really sure if what your answer suggests is correct. You have placed concurrency configuration in Maven Failsafe, a plugin which is responsible for safely terminating the test execution once an exception has occurred therein. Although, as you claim it this has resolved your issue, it might impact your project in the long run.
According to Maven's website, the below code should be placed under Maven Surefire configuration.
<forkCount>5</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
Having followed these guidelines, I was able to successfully run tests in parallel with only updating Surefire plugin.

Maven war packaging, include classes ignores excluded resources

I have the following pom
<project>
....
<packaging>war</packaging>
....
<build>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archiveClasses>true</archiveClasses>
<warSourceExcludes>WEB-INF/sass/**</warSourceExcludes>
<attachClasses>true</attachClasses>
<webResources>
<resource>
<directory>src/main/resources/config</directory>
<excludes>
<exclude>**/*</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
...
</project>
As you can see, I package a WAR while my .class files are not in my WEB-INF/classes folder - they are packaged into a JAR instead.
Now, I am desperately trying to exclude some resources from my JAR - but it does not work. When I run mvn jar:jar - the resources are excluded, however when I run mvn package the resources are there.
Please help.
It seems that #user944849 is right - indeed, the war plugin does not use the jar plugin in order to achieve the JAR packaging.
However, his answer gave me a wrong result still as it will simply create 2 jars - one will be with the resources and the other without. The WAR will still use the wrong one.
The correct answer is to use the new maven resources tag.
The one that corresponds to my configuration looks as follows
<build>
....
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
....
</build>
You do not have the jar:jar goal bound to a lifecycle phase. When you run jar:jar from the command line, the exclusions happen fine; when you run mvn clean package I suspect jar:jar is not executing. Try binding the goal to a phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals><goal>jar</goal></goals>
<phase>prepare-package</phase>
<configuration>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
FYI, the archiveClasses feature of the war:war plugin goal does something similar to what I think you're trying to achieve without requiring a separate plugin config.

How to set up an environment variable in mvn pom?

How can i set up an environment variable (in other words internally accessible by System.getenv("APP_HOME") in a pom file?
APP_HOME=/path/home
I realize i can set it up in .profile, but wonder if pom can do the same trick.
Per bmargulies's suggestion below, i tried the following, without luck
<build>
<finalName>KvpStore</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<includes>
<include>**/*Test*.java</include>
</includes>
<environmentVariables>
<APP_NAME>blah_blah</APP_NAME> <------------------------
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
Some plugins, like surefire, let you set them. There's no way, in general, in the pom.
The doc for surefire is is here. Surefire will set environment variables for the duration of the run of the tests, not for anything else. And you have to make surefire fork.
In the configuration ...
<configuration>
<forkMode>always</forkMode>
<environmentVariables>
<var1>val1</var1>
</environmentVariables>
</configuration>
The documentation of the maven-surefire-plugin show examples and describes how to do such things of setting up system properties.
<configuration>
<systemPropertyVariables>
<propertyName>propertyValue</propertyName>
<buildDirectory>${project.build.directory}</buildDirectory>
[...]
</systemPropertyVariables>
</configuration>
It might be better to use them instead of environment variable, cause it's simpler to use them, cause env variable needed to setup correctly and the cmd.exe and the jvm must be restarted to get them working.
It is not necessary to configure the includes for the tests, cause maven-surefire-plugin has already the following defaults:
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>

Resources