Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run tests after deployment using Maven?

I'm trying to decide how to create a set of Acceptance Tests for a Java-EE web application.

Here's the setup: Maven is used to generate a WAR file and deploy it into Glassfish. On deployment, the MySQL database schema is automatically updated from model classes using Hibernate ("hbm2ddl=auto" option).

The Acceptance Tests need to test the deployed code by invoking various methods and checking the results are as expected(*). We wrote an additional set of packages to hook into an existing system so the Acceptance Tests should show how these can be integrated into the existing codebase.

(*) This may sound more like Unit/Integration Testing but they are Acceptance Tests in the sense that they should prove what we did works and they need to be run after deployment so there is a database in place.

From the above, my current thinking is to use JUnit to check expected values etc. The bit I'm struggling with is how to invoke these tests after deployment. "deploy" is Maven's last phase so not sure if this is possible?

like image 420
Steve Chambers Avatar asked Jan 14 '13 12:01

Steve Chambers


People also ask

Can Maven be used for running tests?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

How do I get my Maven integration tests to run?

The simplest way to run integration tests is to use the Maven failsafe plugin. By default, the Maven surefire plugin executes unit tests during the test phase, while the failsafe plugin runs integration tests in the integration-test phase.

How do I run only test cases in Maven?

If you want to run just a single test instead of all the tests declared in your project, create a Maven run configuration for a single test with the Maven -Dtest=TestName test command.


2 Answers

Just because that phase is called deploy doesn't mean that you have to use it for deploying your application for testing. In fact, it should only be used for "deploying" the artifact to a maven repository. Read through the description of the Maven lifecycle phases and you'll see that there are some phases dedicated to your use case:

pre-integration-test
integration-test
post-integration-test  

Have a look at the Cargo Maven plugin. It's made to deploy your WAR file to various containers for testing. They definitely show demos of use cases like the one you describe on your site. I would expect that ultimately, you can be using Cargo to deploy to your container ( from one of the earlier phases like pre-integration-test )

Note, Jenkins also has a plugin that is a wrapper around the Cargo plugin. So you might do what you need via Jenkins. Also note, you don't need to run your Jenkins build job as mvn clean deploy. You could have one build job that just runs the integration tests, and fires another "deploy" job only when it succeeds.

like image 55
chad Avatar answered Oct 24 '22 08:10

chad


If you really need to do stuff after deployment, then you can either run failsafe, and by implication JUnit) as part of the deploy phase.

What I usually do, if to have seperate module. So, you can have one maven project, which contains your project and a separate 'deployment test' project. Then, building the parent project will build and run your war and then run the deployment tests. You can use junit as normal.

The second fits better into jenkins because you'll still have a single project as well.

like image 45
Matthew Farwell Avatar answered Oct 24 '22 06:10

Matthew Farwell