How can I prevent tests from running during deploy? - java

currently I have surefire configured though Spring Boot's BOM.
I have tests running during deploy though there seems to be some issue with one of them... that said this same test passes just fine in the previous part of the pipeline. I don't actually need these tests to run twice.
I would like to do this as part of my parent BOM (has springs BOM as its parent)
How can I configure tests to not be run during the deploy phase? mvn deploy, running mvn test and mvn verify must continue to work as normal.

You can achieve this while building(install) the archive itself.
We can skip tests while building by two ways:
Using CLI:
mvn install -Dmaven.test.skip=true
Make the same change in pom.xml as:
<configuration>
<skipTests>true</skipTests>
</configuration>
Then give:
mvn install
Both will build the archive without running the testcases.
And mvn deploy, running mvn test and mvn verify will continue to work as normal. Since this command is independent of these three commands.

You can achieve it by using commands while deploying or in pom.xml
To skip the entire unit test, uses argument -Dmaven.test.skip=true
mvn install -Dmaven.test.skip=true
OR in pom.xml
<configuration>
<skipTests>true</skipTests>
</configuration>

Need to configure in skipTests in surfire plugin
<configuration>
<skipTests>true</skipTests>
</configuration>
For more details please follow
http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html

Related

Maven - While Compile the project I need to set a active the profiles

I need to set a active profiles to the jars while compile the maven spring boot project
Following are my two Approaches tried to activate the profiles.
First Approach - Not Activating the Profiles
mvn clean package -Dspring-boot.run.profiles=dev help:active-profiles
-s settings.xml
*
Above command not setting the active profiles while executing jars
java -jar package.jar
I have using maven plugin dependencies for activating the first approach
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions> </plugin> </plugins></build>
Second Approach - Working as expected.
mvn clean package -Dspring.profiles.active=dev help:active-profiles -s
settings.xml
java -jar -Dspring.profiles.active=dev package.jar
I'm using multi-module parent pom structure for the project. Any one pls advise to work the first approach?
Maven is a build system. Its responsible for building your artifact.
Maven's profiles are basically a tool that allows to slightly change the build process depending on various factors (operating systems, version of java and so forth).
These are defined in maven's files (pom.xml)
Spring (and Spring boot of course) as opposed to maven is a runtime framework.
Spring profiles is something totally different - they allow to load different beans, resolve different configurations in runtime (read after you call java -jar app.jar) depending on profile definitions.
So don't be confused with the same name, its only an "accidental coincidence"
Now as for your question.
Maven builds the artifact and packages it for that use spring boot maven plugin. If you want to customize this building process - use maven profiles (and as a consequence help:effective-profiles can be sometimes handy).
When the artifact is built - you can run it. For that you can use spring profiles to define runtime in-variants:
- Addresses of databases
- Credentials
- Some subsystems of your product that won't be run locally
and so forth, this list really depends on the application itself.
There is an option to run the spring boot application right from maven and for that you can really use -Dspring.profiles.active=dev but if you don't run it (and you don't in any of suggested approaches, read about mvn spring-boot:run to understand what does it really mean to run a spring boot application during the build), then:
First approach:
mvn clean package -Dspring-boot.run.profiles=dev help:active-profiles
-s settings.xml
-Dspring-boot.run.profiles is irrelevant here - you do nothing with it (again you don't run the project during the build) so it does nothing
java -jar package.jar
Here you can really specify spring profiles with --spring.profiles.active=dev,whatever
The Second approach:
mvn clean package -Dspring.profiles.active=dev help:active-profiles -s
settings.xml
Again, -Dspring.profiles.active=dev is irrelevant, it does nothing.
java -jar -Dspring.profiles.active=dev package.jar
Here you do specify the list of active profiles (just like --spring.profiles.active, from spring boot's standpoint its the same) That's why the application works in runtime as expected

How to Skip All Integration Tests (-DskipITs) and One Unit Test mvn Command Line

I am relatively new to Maven. I have done a lot of research and digging on this topic, but I can't seem to find an answer, so I thought I would ask here.
Goal: I would like to run mvn clean install test while skipping integration tests, as well as one particular unit test class.
I have tried the following:
mvn clean install -DskipITs -Dtest=!MyTestClass test
mvn clean install -DskipITs&&test=!MyTestClass test
mvn clean install -DskipITs&test=!MyTestClass test
However, none of the above commands seem to work. The first command of the three above made the most sense to me, but it seems as though the integration tests are being run when using that command. This is where my knowledge and understanding of Maven has a gap; I'm not sure if that's the expected behavior, or if that is the appropriate way to pass multiple properties on the command line?
When I run this command: mvn clean install -DskipITs test, the integration tests are successfully skipped.
I am familiar with the Maven build life-cycle, but it is possible that I am misunderstanding something or missing a detail.
Integration tests with maven are normally run with maven-failsafe-plugin
To tell this plugin to skip integration tests (make sure your integration test class names follow the convention *IT.java, otherwise you need to include them with <inclusions>), you can do that in the plugin's configuration, or from the command line (official doc):
mvn test -DskipITs
Single tests can be skipped with:
mvn test -Dtest=!MyTestClass
So this should work:
mvn clean install -DskipITs -Dtest=!MyTestClass
What worked for me was the following command:
mvn clean install -DskipITs "-Dtest=!MyTestClass, !**/*IT.java" test
I am still learning Java, but here is what I think happened in my case.
There are two plugins pertaining to testing in Java (there are probably many more, but these two were relevant to my issue): one is called "maven-failsafe-plugin", while the other is "maven-surefire-plugin". As #hovanessyan and others have pointed out, maven-failsafe-plugin typically runs integration tests, while maven-surefire-plugin typically runs unit tests (Maven docs reference).
In my case, when I would run the command mvn clean install -DskipITs -Dtest=!MyTestClass test, upon further digging in the logs, the integration tests would fail and I would receive the following additional buried error message:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.21.0:test (default-test)
The integration tests for the project are found in a directory called integrationtests, and are named according to the convention "MyIntegrationTestIT.java". What eventually led me down the right track was this: Surefire docs. These docs describe the "test" argument that you can pass with a Maven command.
It seems to me that when I passed the argument -Dtest=!MyTestClass, it's as if that instructed the Surefire plugin to "don't run MyTestClass, but do run every other test file." Meanwhile, the -DskipITs argument instructed the Failsafe plugin to skip integration tests (which it had been doing all along). When I explicitly called out the test files that I didn't want to run, in the form "-Dtest=!MyTestClass, !**/*IT.java", Surefire understood exactly what I wanted to do. The Surefire plugin ran every test with the exception of MyTestClass and the integration tests, and the Failsafe plugin skipped the integration tests.
I don't fully understand why, in my case, the Surefire plugin was running the integration tests in the first place. Maybe it has to do with some config setting in the codebase I'm working with, or the naming convention of the integration test files, or some annotation (I'm still learning a lot about these things). I am sure that this answer could be edited to include even more helpful information or context that I don't yet have. In any case, hopefully these learnings are helpful for some other folks experiencing this issue.
Finally, it helped a lot when debugging to run the command mvn help:effective-pom and pass -X along with my mvn clean install test command.
This configuration works for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>%regex[.*TestIT.*.class]</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>%regex[.*TestIT.*.class]</include>
</includes>
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

Spring boot doesn't run unit tests

How can I run unit tests for spring boot application while building and deploying using spring boot:run command.
My expectation is to have all my unit tests executed before running application, but I dont want to make another maven command like mvn test before.
My problem:
I made a simple spring boot application and I could'd find a way to run unit tests while running application from intellij or from command line. Firstly I thought that maybe I have wrong configuration or wrong names of test classess or maybe wrong project structure. So I created spring boot application from intellij template. To my happiness it had already default test written so I simply run application. Unfortunatelly test was not executed.
This is a screenshot of project structure, pom.xml, main class and unit test created by intellij.Project created by intetelij
I changed the test runner and test to fail and tried again. Same result.
unit test changed to fail
I googled what is hidden underneath spring boot:run command here
http://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html
I found something interesting at the top of manual: "Invokes the execution of the lifecycle phase test-compile prior to executing itself." So my understanding is that this command only compiles tests but not run them? If So, the question is - Is it possible to add "test" phase by adding some flag to the command?
Your problem here is to do with the maven lifecycle. According to the docs for the spring-boot:run, it binds to the lifecyle phase validate by default, and invokes the phase test-compile before executing.
What you're asking for is to execute the tests before running the application. You could do this with a custom maven profile in your POM - something like the following.
<project>
<profiles>
<profile>
<id>test-then-run</id>
<build>
<defaultGoal>verify</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>spring-boot-run</id>
<phase>verify</phase>
<goals>
<goal>run</goal>
</goals>
<inherited>false</inherited>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
...
</profiles>
...
</project>
With this in your POM, you could then run the tests and start the app with:
mvn -P test-then-run
This binds the run goal to the verify phase instead of the validate phase, which means that the tests will be run first. You can see which order the phases are run here: https://maven.apache.org/ref/3.3.9/maven-core/lifecycles.html
I knew that I could use mvn test before running application. In fact it can be achieved by many ways in intellij. For me, I just add mvn goal test before launching application:
spring boot configuration in intellij.
But of course it may be done differently.
The thing is, I was just very curious whether I could somehow manipulate spring boot:run command by using some flag or sth else to get same results but its impossible.
olambert, thank You for Your answer too, it works very well.

How can I run my Selenium tests with Maven? [duplicate]

This question already has answers here:
How do I run a selenium test using maven from the command line?
(4 answers)
Closed 7 years ago.
I'm trying to figure out how to run Selenium WebDriver tests without having to use Eclipse or IntelliJ or any other IDE. I do all my java development using a plain text editor, and don't want to have to install (and learn) an IDE just for the sake of compiling and running tests.
I've tried following the Selenium documentation, but it stops short of actually telling you how to run the tests from the command line.
My brief experience with maven amounts to the following:
$ mvn compile
<snip>
No sources to compile
$ mvn test
<snip>
No tests to run
$ mvn run
<snip>
Invalid task 'run'
The only other one I know is mvn jetty:run but that doesn't seem right as I'm not wanting to run a new web server.
I suspect I just need to set the correct targets etc in my pom.xml, but I don't know what they should be, and surprisingly can't find any online.
Can anyone help please?
In short:
mvn integration-test or mvn verify is the thing you're looking for.
Explanation
The goals, you're invoking, are lifecycle phases of maven (see Maven Lifecycle Reference). mvn test is intended for standalone unit tests, mvn integration-test runs after compiling, testing and packaging. That would be also the phase, where you invoke Selenium tests. If you need to start and stop Jetty, Tomcat, JBoss, etc., you would bind start/stop of these to pre-integration-test and post-integration-test.
I usually run my integration-tests using Failsafe and perform there invocations to Selenium and other integrative tests.
OK, I finally figured it out after realising that this is actually a Maven-specific question rather than Eclipse or Selenium.
Maven can be made to run the code it compiles by using the exec-maven-plugin and adding the following to the pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>Selenium2Example</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
As you can probably gather from the snippet, arguments can be passed in by listing them in the pom.xml. Also, be sure to use the proper package name in the mainClass element.
You can then run mvn compile followed by mvn test to compile and run your code.
Credit has to go to http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/ for listing several ways to do this.

Easier way to run a Java application from Maven?

If I have understood correctly, I need to type this to run my project from maven:
mvn compile
mvn exec:java -Dexec.mainClass="com.foo.bar.blah.Main"
Is there a way I can make this simpler? Optimally I would like to just do
mvn run
1) Create a new profile called "run" (or another name of your choice)
<profiles>
<profile>
<id>run</id>
2) Set the profile's default goal to "verify" (or you can choose "install", choosing a phase after compile will ensure that the code will automatically be compiled before running the class)
<profiles>
<profile>
<id>run</id>
<build>
<defaultGoal>verify</defaultGoal>
3) Add the exec-maven-plugin to this profile (see this), but configure it to run in the 'verify' phase.
<execution>
<phase>test</phase>
4) You can now run your class using the following:
mvn -Prun
A little more configuration, a little less command line parameters ;-)
using the very same exec:java plugin, you can configure your task in the pom.xml, then execute it in a simpler fashion by mapping your goal to the run step of the lifecycle, like this example shows.
As the above example shows, you can wrap that plugin into a separate profile. Take a look at the 3rd solution
Sadly, no `(as far as I know). If you ahve a web application you could use Jetty plugin to run it doing:
mvn jetty:run
but for standalone apps, you need the exec plugin.

Categories