mvn package executes more tests locally that in bamboo - java

I have a spring-boot application which includes JUnit tests and I build using maven. I have organized by JUnit tests in a Suite using #Suite.SuiteClasses notation.
My problem/question is why when I run mvn package locally, I see that the unit tests referenced by the Suite are executed but they are also executed as if they were not part of the suite, while building the code in Bamboo, using again mvn package executes the tests only once (i.e. as members of the suite).

Do you use the same profiles / settings / maven commands on bamboo and locally?
I think what happens is that when executing the package phase the surefire-plugin starts executing all the tests. There might be a naming issue with the includes the surefire plugin uses by default.
If you execute the maven goal with -X you should be able to see the surefire-plugin configuration it uses to identify the tests. This should only match your suites - not the suites and the tests themselves.

Related

How can i run only the unit tests not the integration tests on build phase with maven

I added the maven-failsafe-plugin to my project for runnig integration tests. When i run test only unit tests are running and when i run integration-test first unit tests and then integration tests are running.
It's all what i want so far but when i clean and build the project both unit tests and integration tests are running which i wouldn't prefer because i just don't want to prepare the environment for integration testing on every build.
I clean and build the project for some generated source to regenerate and i don't want to run the integration test meanwhile.
Is there a way that i only run the integration tests on purpose not by any automation?
I added -DskipITs flag clean install command of maven so integration tests are not running when i make a call of clean and install.
And i created maven goals as integration-test and verify to run and verify the integration tests via the failsafe plugin which i added to my pom.xml.
As i'm using netbeans as ide, i set the predefined maven commands via the actions tab of the project properties and i can run the integraion tests via right click to peoject and integration test sub menu under maven menu.

How to run single integration test in maven?

I am trying run single integration test. I have a a lot of *IT class and I want to run only one test. I try this :
mvn -Dit.test=XControllerIT verify
Am I doing wrong? Is there another alternative to this? Maven is being used.
There are two main options depending on your project setup:
Integration Tests are run with a dedicated Failsafe plugin
Integration Tests are run with a regular surefire plugin
If you have a failsafe plugin (and you actually should, its a recommended approach), then use the following snippet:
mvn -Dit.test=MySampleIntegrationTest failsafe:integration-test
If you're on surefire, then run:
mvn -Dtest=MySampleUnitTest surefire:test
In both cases there is a direct plugin goal execution, bypassing the lifecycle like in your initial example (with mvn verify)
In maven it is possible to run the lifecycle, see Default Lifecycle Documentation for more information
Basically, the lifecycle is comprised of phases with plugins bound to each phase
So when you run the mvn verify all the phases before verify will also run.
As a consequence, the code will be compiled (compile phase with a maven compile plugin automatically attached to it will do the job), tests will run (surefire plugin), and so on.
If you don't have a compiled source code and code of tests, you can't use the presented approach because you have to compile the code first.
However, if you already have everything compiled, it makes sense to run only the one test without recompilation of the code, and in this case, depending on the plugin you can use the suggested solution.
Especially it can make sense for local debugging or for CI in some cases multi-step build setup (can be seen in fairly complicated projects)

Cucumber tests are being skipped

I am using cucumber framework in my project. So for the reporting part, i need have started building the cucumber reports with jenkins.
So during the setup for the trail build i am facing the below issues.
All the tests are skipped, but not executing.
Can anyone have solution. I am happy to provide any other inputs required.
When using maven and cucumber you have two options to run the tests.
With maven: mvn clean install
With cucumber:-Dtest=Runnerclass test
Where Runnerclass is the class name of runner.
Include surefire plugin in your POM.xml to pick your test. if you want to run test as well, then use mvn clean install.
Refer
How to run Cucumber JVM Test in Jenkins

Integration tests not getting executed

I have some junit tests and htmlunit integration tests in one of my maven project. The problem is that my integration tests are not getting executed when I run
mvn clean test
Junit Tests Here:
webstore\src\test\java\com\istore\dao\AddressTest.java
Integration Tests Here:
webstore\src\test\java\com\istore\presentation\htmlunit\PageTests.java
How does mvn determines that AddressTest.java should execute and other one should not?
http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#includes describes the expected filepatterns.
btw, if you are talking about integration tests, have a look at the maven-failsafe-plugin. It uses *IT.java as file pattern
The problem is suffix Tests, should be Test in your PageTests.java!
By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:
**/Test*.java - includes all of its subdirectories and all java filenames that start with Test.
**/*Test.java - includes all of its subdirectories and all java filenames that end with Test.
**/*TestCase.java - includes all of its subdirectories and all java filenames that end with TestCase.
Maven Failsafe Plugin is for integration tests and uses suffix IT. To invoke integration tests with Failsafe Plugin use
mvn verify
References:
Inclusions and Exclusions of Tests in Surefire
Inclusions and Exclusions of Tests in Failsafe
Problem resolved after executing following command:
mvn failsafe:integration-test

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.

Categories