Skip integration tests in maven by directory - java

Our project structure looks something like this (sorry about the formatting):
server/
pom.xml
src/
main/
java/
test/
java/
it/
java/
I would like mvn clean test to skip all the tests in it/java regardless of the name of the test. The solutions I have seen depend on integration tests being in a different package or having some prefix on the name to indicate that they are integration tests.
Is there a way to exclude it/java from running when I run mvn clean test? Eventually we want to be able to run the integration tests with by running mvn integration-test.

Your best best is probably to make it a multi-module project, and then in the it module skip tests based on different maven properties. This would allow you the flexibility that you're looking for.

I don't think src/it/java is a folder known to maven so if you put your tests in there they will be ignored simply because its a random folder to maven? :)
Anyway. To execute integration tests I recommend using the maven-failsafe-plugin which was build for this: http://maven.apache.org/surefire/maven-failsafe-plugin/
It does also rely on some naming convention to distinguish integration tests from standard ones but I could re-configure the testSourceDirectory. But I think the classes may need to be compiled first if you change that folder (but that should be possible).
The unit tests are then configured to be executed by the maven-surefire-plugin.
What I usually to is have the surefire-plugin execute all tests but exclude the IntegrationTests (using a pattern in the configuration) and the failsafe-plugin include those but exclude the standard tests. But I have the tests named differently so I can use the conventions for the folders.

Related

Use Karate framework with Fail Safe and Sure Fire plugin [duplicate]

Is there a way to run Karate test during maven's integration test phase? It seems that surefire plugin is hardcoded into Karate. I have tried to override it using failsafe plugin but with no luck. I don't want test to run along with unit tests.
Thank in advance!
It seems that surefire plugin is hardcoded into Karate
I'm not sure where you got that impression, but no, the surefire plugin is not hardcoded into Karate.
Keep in mind that the simplest way to not run a JUnit test via surefire is to not use the *Test.java naming convention.
I think the solution for you is simple, whichever JUnit test is the "entry-point" for your Karate tests (the parallel runner is recommended) - just use the failsafe naming conventions.
And then, just include the failsafe plugin as per the examples and it should work. If you have trouble getting that to work (unlikely), then you should look at maven profiles.
EDIT: also see this comment: Is there a way to run Karate tests as an integration test suite against a pre-booted spring boot server?
Turns out that I cannot be done and it is a limitation of Maven, not Karate. Howto add another test source folder to Maven and compile it to a separate folder? - Here is my test project to prove it out: https://github.com/djangofan/spring-boot-hello - Thanks for leading me down what appears to have been the correct path to discover the limitation. Using Gradle would likely solve my issue but that is not an option on my project. If I use Karate for "separated integration tests", I need a separate mvn test module.

Maven: Finding out if tests passed or failed after using maven.test.failure.ignore=true

I'm trying to run a complete Maven build of multiple projects for an automated build tool. If unit tests fail, but the project itself builds correctly, I want to be able to continue the build and detect this after the build completes. I tried doing this:
mvn clean package -Dmaven.test.failure.ignore=true -Dmaven.test.error.ignore=true -Dmaven.test.reportsDirectory=/Users/bfraser/misc/reports
The "maven.test.failure.ignore" and "maven.test.error.ignore" properties work fine. However, surefire seems to ignore the "maven.test.reportsDirectory" completely (in fact, if you look at the documentation for the test goal, the reportsDirectory property is not documented to be tied to the system variable). This may be because I'm building a multi-module project? All reports seem to go in the target/ folder of the subprojects.
It is very difficult for me to be able to edit the POMs in an automated way since many of them have parent POMs that might be on a Nexus repo somewhere, etc. -- I need to be able to do this externally to the project (preferable via command line switches, but if I need to create some files so be it... as long as I don't have to edit the project POM it's all good).
I just need to know if any test failed. I'm not particularly fussy about what/how many tests failed.

Java How can I use a maven sub project for my integration tests?

I would like to make a separate project for my integration tests.
My current structure is like this (Maven project):
my-project-parent
my-project-lib (jar)
my-project-web (war)
Inside the lib sub project is the logic of my project. Inside the web sub project is everything that is related to my Servlets. There is still a lot of logic in there that makes it possible to handle all the different requests.
The sub projects have unit tests. Now the unit tests and integration tests are scrambled in the project and it’s hard to see if it is a unit test or an integration test. By putting the integration tests in a separate project it also makes it possible to manage whether to run the integration tests.
Because of this I would like to create another sub project with all the integration test (my-project-it).
So far I have a separate project called “my-project-it”. I can do some test on the lib sub project. The problem I ran in to was that I needed a lot of classes from my-project-web. I’m unable/not willing to put the war as a dependency.
Can anyone help me with this?
There is an (old but) good wiki article about integration testing with maven.
I personally prefere a separate module that contains all the integration tests in src/it/java. This module can be activated with a maven build profile. The tests are run and the results verified with the failsafe plugin.
hth,
- martin
I would move all classes that are needed for my-project-it from my-project-web to my-project-jar and made it my-project-it's dependency.
Another option is instead of creating a separate my-project-it project create a separate package for integration tests under src/test/java and execute them with maven-failsafe-plugin http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html

What is the process with Maven project to compile and test the code?

I have maven project imported in my eclipse. Now I need to start making changes to it and test it with the integration test (out of App server). Currently, the integration test is run out of server using openEJB container.
My basic question is, what is the regular process to compile, build and test with Maven?
mvn install
Maven -> Update Project.
Run my test from command line
Is it how it is done? I am specifically interested in knowing mvn install commands.
So should I do all three steps before I can test it?
Example: I just wanted to print something and see what is the output. For this I guess I need to do all these steps?
The openEJB container needs classes so it can load them.
There is a wonderful Maven quick-reference sheet at http://maven.apache.org/guides/MavenQuickReferenceCard.pdf
First, you should be aware that unit tests and integration tests are separate and are run from separate plugins and at separate parts of the maven lifecycles. Unit tests are run with surefire and integration tests are run with failsafe.
You want to run integration tests and the failsafe documentation says:
NOTE: when running integration tests, you should invoke maven with the (shorter to type too)
mvn verify
rather than trying to invoke the integration-test phase directly...
This is the best way to run integration tests directly in maven. It will run all the preceding steps necessary (eg: compile) in order to run the integration tests. It won't waste time doing an install because install happens immediately after verify.
But if you're running the tests locally, it may be a better idea to run your integration tests directly in your IDE. That will give you a much faster feedback loop.
If it is Eclipse project the most reasonable thing is to do everything not from command line but from Eclipse. Assuming you have m2e plugin installed, go to your_project->run as->Maven test and run it.
You dont need neither install nor package phase to run Maven tests, package will create a jar which is not needed for tests, install will copy this jar to local repo which is also useless. When Maven run tests it uses compiled classes from target dir and ignores project's jar if even it exists.
Yes, mvn isntall is the most popular option. It compiles, packages and tests your project.

How to run unit tests in multi-module maven setup if i put the tests in a separate module?

i have created a multi-module maven project and i am trying to write and execute some tests on specific modules. I am trying to put all test-code into a separate module, but i am wondering if this is the correct way and if so how to i setup the maven build/test cycle so mvn install will use these tests?
According to the Maven Standard Directory Layout test classes belong to the directory src/test/java and required resources to src/test/resources. On the long term it will make your life easier by following the rules, especially when you work on many different projects (where you sometimes can not define alternative structures). In this setup the test cases are automatically invoked by mvn install or mvn test.
You find more informations about the surefire-plugin (responsible for executing the tests) here.

Categories