How to run single integration test in maven? - java

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)

Related

Check targets of a specific phase of Maven build lifecycle of a project

I would like to know if there are tools that tell you "what is run in each specific phase" of the Maven Build Lifecycle (for example if I set JUnit to run in <phase>test</phase, this tool should tell me that during test, JUnit is run).
One example is the BuildPlan Maven Plugin, but for example I can't say "ok show me only the phase integration-test"
Or, in case, my real question would be "how to spot integration test in a Maven project", but I think it depends(?)
See buildplan:list-phase's <phase> parameter:
<phase>
Display plugin executions only for the specified phase.

How to make a plugin goal run regardless of test failures

I have a plugin that is supposed to send files to a database regardless of test failures. I tried setting the fail-at-end setting but the plugin still didn't run. ignoring failures altogether doesn't seem to be an option for me in this case because then faulty jars would be deployed to the artifact repository. I can use this plugin for as a separate command line script but it would be optimal if it could be put in the pom.
The tests need to be run before the plugin does, because the plugin gets the test report files.
I am aware that there are similar questions to this that have been asked before, but they don't seem to help me so I was wondering if someone has similar advice.
If the plugin has to run after the tests, the tests just can't fail the build.
Maven Surefire plugin has an option: maven.test.failure.ignore to run the tests but keep execution
See Here a full description of Test MOJO.
This should be the first bet.
Another option is to build just like this, but run the plugin "externally" (a kind of two steps build). Run maven install or whatever you need, and then (separately)
mvn <group>:<artifact>:<version>:<mojo of your plugin>
try mvn your-goal -DskipTests - will ignore testing.

How to do a deployment pipeline with Maven

We want to accelerate our build pipeline for a multi-module Java web application, which roughly consists of
compile/code analysis
unit tests
integration tests
GUI tests
At the moment each of these build steps starts from scratch, compiling and building again and again, which costs time and means that we do not deploy the actual files to production that have gone through the tests. Is it possible to get Maven to not recompile everything on subsequent steps but instead run the tests against the previously compiled classes?
We are using Maven3 to manage our dependencies and Teamcity as a build server (7 at the moment, planning to upgrade to 8 soon). I have tried to set up a build chain, doing a
mvn clean install
on the first step and then exporting all the */target/ folders to the following builds. Then ideally I would only do a
mvn test
mvn integration-test
Unfortunately I have not been able to persuade Maven to do this properly. Either it compiles the classes again or produces errors.
Has anyone successfully done this kind of setup and has any pointers for me? Is this possible with Maven and is this even the right way to do things?

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