Do not run integration tests when running mvn clean install - maven

I want the following to happen:
when I run mvn clean install , I want unit tests alone to run (no integration tests)
When I do mvn integration test , I want integration test alone to run (no unit tests)
when I do mvn test unit test alone should run.
I tried few things with Maven Surefire plugin and Maven failsafe plugin but could not
achieve this. What I have tried is: added Surefire and Failsafe plugins, tried separating the unit tests with annotations, as well separating unit and integration tests at package level, and keeping specific naming convention for unit and integration test.
What I am able to achieve is run unit tests alone on mvn clean install, but when I run Maven integration test I am not able to stop unit tests from running. Any idea how I should go about it?

The problem you have that you don't understand the Maven life-cycle which has the following phases (only excerpts):
clean
...
compile
...
test
..
pre-integration-test
integration-test
post-integration-test
..
install
This means in other words your requirement to do mvn clean install without running the integration tests is not satisfiable by the defaults (conventions).
I would suggest to create a profile where you put the maven-failsafe-plugin into so you can control if integration-tests will run or not. This results into a thing like this:
mvn -Prun-its install
to run the integration tests you can use the following:
mvn verify
which will include running the unit tests but this can be suppressed by using:
mvn -DskipTests=true verify
Using mvn test will run only the unit tests no integration tests cause it's earlier in life cycle.

Related

What is the difference between 'mvn verify' vs 'mvn test'?

I'm bit confused with mvn verify phase. I've created a Spring Boot project (a simple project, without any explicit configurations added). I've created a few JUnit unit tests which are run with both the mvn verify and mvn test commands.
There isn't any difference observed in the mvn verify and mvn test command output.
What does mvn verify do different than mvn test?
Also some posts on Stack Overflow mentions that mvn verify runs the integration tests. If this is the case then I have few questions.
How does Maven identify a specific test as a unit test or integration test?
If mvn verify is supposed to run only the integration tests, then why are unit tests executed with it?
First of all, when you run a Maven goal, it will run any previous goal. The order of basic phases is:
Validate
Compile
Test
Package
Verify
Install
Deploy
If you run Test, Maven will execute validate, compile and test. Based on this, the first point is that verify includes test.
Based on official documentation:
TEST - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
VERIFY - run any checks on results of integration tests to ensure quality criteria are met
To run unit tests, the Surefire plugin is recommended. And Failsafe for integration tests.
The verify command executes each default lifecycle phase in order (validate, compile, package, etc.), before executing verify. In most cases the effect is the same as package. However, in case there are integration tests, these will be executed as well. And during the verify phase some additional checks can be done, e.g. if your code is written according to the predefined checkstyle rules.
Conclusion: if you want to run your integration tests and check it, use verify. If you only want to run unit tests, use test.
My personal advice: if in doubt, use verify.
How does Maven identify a specific test as a unit test or integration test?
Integrations Test always takes a name like IT.java or IT.java or *ITCase.java

Maven run command for specific test suite

i am trying to build maven command to run specific test:
i want to be able to execute this:
mvn test
mvn integration
mvn specificdata
so each test will go into a folder and run the suite
src/test
src/integration
src/specificdata
mvn test works with the test folder but when i run
mvn specificdata i get
[ERROR] Unknown life-cycle phase "specificdata".
same for integration
how can i make mvn run these tests independently?
This cannot be done in the way you describe it.
test is a phase and as such, part of the standard lifecycle. Calling mvn test does not only run tests, but executes the phases before test as well.
The standard lifecycle also offers phases for integration tests, esp. integration-test. Integration tests are usually put into src/test as well and distinguished by a naming convention. But beware: Calling mvn integration-test will call all previous phases (including test, compileetc.) as well.
https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

How to configure build step for maven goal in teamcity to run unit test

I use junit in my java project (developed using intellij idea) for unit test, and I want to configure build step in team city to run my unit tests only. I also use maven to build my project. It works when I set goals for maven to "clean compile" but I dont know how to configure build step to run unit tests.
Also in command line. when i run "maven test" it runs unit tests correctly and shows the failures.
I can't comment on TeamCity, but I'll try to help anyway out of my maven knowledge.
So first of all mvn clean compile will never run your unit tests, because maven has a concept of lifecycle and testing phase is coming after compile phase.
When you run mvn test it will run all the face up to (including) the test phase so the unit tests will run as a part of maven default lifecycle.
Now you're asking about a "build step for running unit tests" from which I conclude that you need a separate step. In maven phase is nothing more than running a series of plugins. In maven plugin has goals, so you can run a plugin responsible for running unit tests directly.
In maven this plugin is called "surefire" and a goal is called "test", so you can run:
mvn surefire:test
Given the classes (production code and tests) are compiled, you will see that this only runs your unit tests. So this probably has to be configured in Team City.

Maven is skipping unit tests

I am facing an issue while running test using command line.
mvn test -DskipTests=false -DskipITs=false is only running some unit tests and skipping the rest. Are there any tricks or other options to force maven to take into account all the related tests.

Maven: disabling test phase but not integration test phase

I'm working with a maven project, and are trying to run only the integration tests, while skipping unit tests.
I'm running the integration tests using failsafe plugin, and the plugin execution is bound to the integration-test phase.
Is there any way I can run only the integration tests, while skipping the unit tests (which are run via surefire).
My googling led to a page that shows me how to do the opposite.
If you want to skip the test you have two choices:
mvn -Dskip=true ...
which will not compile the tests nor execute them (logical ;-)). The other option is just to skip the execution of the tests but doesn't skip the compilation of the tests
mvn -DskipTests=true ...
But you can't skip the test lifecycle phase in Maven.

Resources