Possible to Skip Junit tests only in Jenkins? - spring-boot

Currently our companies Jenkins deploy process isn't set up to read external property files for testing. I would still like to write and run tests locally and then have tests be skipped when pushed to github (where a jenkins process gets kicked of to build the app and push it into a container).
Is it possible to programmatically tell surefire pluggin to only run in a local environment but not to run in the Jenkins pipeline?
I know I can use the following config:
<configuration>
<skipTests>true</skipTests>
</configuration>
but this means I need to remember to comment and uncomment each time I push, and I would rather not do that.
Thank you

The answer of #NullPointerExeption is an option.
You can also use maven profiles for that.
e.g.
<profiles>
<profile>
<id>jenkins</id>
<activation>
<property>
<name>jenkins</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
When a developer run tests they will be executed as skipTests is false by default
From Jenkins use
mvn clean verify -Pjenkins
This will skip tests.
In general it's good to have a jenkins profile and place all the staff that it's related to jenkins environment in this profile
More on profiles here

You can specify the build in your jenkins file to skip the test during build stage.
This will ensure your project is build skipping tests
stages {
stage('Build') {
steps {
sh 'mvn clean package -DskipTests'
}
}
}

Related

Show Maven Profile

I want to build a spring-boot Java project without running the integration tests. I'm taking the approach of adding annotation
#Category(IntegrationTest.class)
and defining interface
public interface IntegrationTest {}
I have this in my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<skipTests>${maven-surefire-plugin.skipUnitTests}</skipTests>
<excludedGroups>
com.test.annotationtype.IntegrationTest
</excludedGroups>
</configuration>
</plugin>
When I run mvn install, it runs the integration test.
It's a multi-profile pom.xml, and I'd like to at least verify that the profile I think I'm running, really is the one actually run.
<profile>
<id>woodsman-default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
...
</profile>
When I run
mvn -debug install
no where in the log do I see the profile id woodsman-default. How can I get Maven to show me which profile it decided to use?
Bonus points for suggesting why it's not ignoring integration tests, but I'd settle for the easy answer.
Thx
Woodsman

How to configure maven for executing test ONLY on deploy

I have maven surefire plugin on pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>org.mycompany.service/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Now I want maven to execute test ONLY on deploy goal, therefore:
When executing mvn deploy tests should run
When executing mvn package or mvn install, tests should not run because goals are prior to deploy
The only way I can think of to make this work would be to bind the surefire plugin to the deploy phase. This has drawbacks:
The tests are run after the artifacts was installed.
Maybe the tests break because they are not meant to be run that late in the lifecycle.
I am not sure how to make sure that they are executed before the deployment happens.
Do you want this behaviour by default, or do you want that just for you or in certain environments?
Adding -DskipTests to the command line is usually good enough.
I have some CLI aliases that I use on my local workstation, so I don't have to type all of it, and don't forget to add it. See e.g. for zsh: https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/mvn

how to run integration tests with maven build from eclipse or from command line

I created a integration test class "sampleIT.java",to test this class, I added run configurations for junit in eclipse under vm arguments like
-Dspring.profiles.active=it-dev
-Dspring.config.name=it-client
the test is running successfully.
I was trying to run maven build and it should also cover(run)integration test class. I added run configuration for maven build, added goals as
clean verify -Dspring.profiles.active=it-dev -Dspring.config.name=it-client
I added these contents in pom.xml
<profiles>
<profile>
<id>it</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/*IT</include>
</includes>
<argLine>-Dspring.config.name=it-dev</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
I tried in command line as well, but the integration test is not running
how to run maven build to run all junits and integartion tests in eclipse or from command line?

If argument in maven Surefire plugin, skip E2E tests

I couldn't seem to find anything on this but I'm curious if I can pass an argument during runtime to skip all of our projects E2E tests.
Is there anyway for me to do something like the segregated exclude block in the following pom example?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
<excludes>
<exclude unless="${skip.E2E.tests}> **/*E2E*.java</exclude>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
Then I could just call mvn clean install -Dskip.E2E.tests=true. Anybody seen anything like this?
I suppose I could do something like...
<exclude>${name.of.tests.to.exclude}</exclude>
and then mvn clean install -Dname.of.tests.to.exclude=**/*E2E*.javabut I would prefer to get an easy true or false argument to set rather than this in case some of the tests I want to skip do not include E2E and I need to add them to a list.
It's hard to tell just from the snippet of your pom that you are showing, but it looks like you are using surefire for both your unit and your e2e tests. Instead, you should consider using the failsafe plugin for e2e.
One benefit is that the e2e tests will run in a different stage so you get the behavior looking for by default. They are run during the verify stage of the project build. So, you can run mvn test to run unit tests only.
You can configure your project to use fail-safe like this:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
Run them using: mvn verify
Running mvn install -DskipITs will skip only integration tests, while still running unit tests.
And running mvn install -DskipTests will skip both integration and unit tests.
If you want to implement such a condition, you could use Maven profiles and have two configuration:
The default one as part of the normal build, not skipping the E2E tests
the profiled one skipping them
The profile could be then activated upon property or direct activation.
As an example you could have:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>skip.E2E.tests</id>
<activation>
<property>
<name>skip.E2E.tests</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
<excludes>
<exclude>**/*E2E*.java</exclude>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Note: the default Maven Surefire Plugin applying to normal build and then a profiled one.
Running:
mvn clean install
Will not activate the profile and your build will skip the tests. While running:
mvn clean install -Pskip.E2E.tests
or
mvn clean install -Dskip.E2E.tests=true
Will activate the profile and as such add the exclusion to the tests execution.
So this is exactly the scenario you were looking for, I presume.
Alternatively and as suggested by #AndrewEisenberg in the another answer, you could use the Maven Failsafe Plugin for different type of tests. The main two differences are that: it has different phase bindings AND when it fails, it does it in a safer way. As from official documentation:
If you use the Surefire Plugin for running tests, then when you have a test failure, the build will stop at the integration-test phase and your integration test environment will not have been torn down correctly.
The Failsafe Plugin is used during the integration-test and verify phases of the build lifecycle to execute the integration tests of an application. The Failsafe Plugin will not fail the build during the integration-test phase, thus enabling the post-integration-test phase to execute

How to skip unittests when using mvn scm:bootstrap

I'm trying to use the mvn scm plugin to check out the daily tag, and create an assembly from that version of the code. I configured the scm plugin and everythhing is working well, except that I can not seem to tell it to not run the unittests.
I tried:
Passing the -Dmaven.test.skip=true command line parameter
Creating a profile where the surefire plugin skips test, and list that profile in the scm configuration "profiles" section
setting the "maven.test.skip=true" as an environment variable
In all cases, when the scm plugin starts running the goals I told it to run in the configuration (see below), it also runs the unittests.
Below is the example I used to skip tests by using a profile:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.0</version>
<configuration>
<goals>install,assembly:assembly</goals>
<profiles>skiptest</profiles>
</configuration>
</plugin>
And this is the profile (I defined this in the pom.xml of the project):
<profiles>
<profile>
<id>skiptest</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
The command I use to do the checkout and bootstrap is:
mvn scm:bootstrap -DscmVersion=daily-20110427-421 -DscmVersionType=tag
I'm running mvn 2.2.1 on a Linux machine, and doing a checkout from a CVS repository. It's an existing project, I have continuous integration and tagging all up and running, I just want to check out a daily tag and create an assembly from that.
Any tips are much appreciated.
Edit: Got it to work with the answer below, but only after I upgraded to maven-scm-plugin version 1.1. Apparently, 1.0 did not propagate profiles.
Try this in the profile:
<profiles>
<profile>
<id>skiptest</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
</profiles>

Resources