views:

48

answers:

2

Hello,

Let me explain some of my constraints.

We have a war that has a CXF Soap service and a Spring MVC REST Service. Both the CXF and Spring MVC implementations are in a separate jar and are brought in as dependencies. The REST service has its unit tests in its project.

I was wondering if there was any way to, while doing something like 'mvn clean test' in the REST jar, to have a local version of the war set up and then run the unit tests. Thus, when building in something like Hudson or doing releases, there won't have to be any workarounds (such as deploying a snapshot ear or running a local war manually)? I've seen this done when the tests are within the war using cargo, but not when the tests are separate from the war.

Right now, we're going to take the tests out into a separate jar but that's still not ideal as if something happens to go wrong during a release, that'd mean the REST jar and war were already released. I'd prefer do it the above way, with the tests in the same project as the REST service.

If anyone has any pointers or doc or examples that could help with this, it would be appreciated.

+1  A: 

Maven has also integration-test phase. Use maven-jetty-plugin to start container and deploy application. Then run your integration test.

Update

Tests cannot be in a jar file. Maven surefire plugin could not run them. The tests are part of the project where you run integration test. The tested war file can be set as dependency library. It will be downloaded, deployed and the you run integration tests.

amra
Doesn't that only work if the tests are with the jar? Or am I not seeing something there?
AHungerArtist
See my update...
amra
Sorry, I meant war, not jar. That's the thing, I have to have my tests separate from the war (which means being in a jar). Thus, it sounds like this won't work for me.
AHungerArtist
You can have integration test as part of the war project. It's even simpler to configure. You have to setup jetty plugin to start in integration phase and setup surefire plugin which test should be used for integration testing.
amra
I suppose I'm not making myself clear. The jar is separate from the war and contains the tests. What I would to do is somehow have the jar make a local version of the war to run its tests.
AHungerArtist
I see. The only possibility is to configure Surefire plugin. Take a look what this mojo enables: http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html You must unpack test-jar and then setup testSourceDirectory.
amra
I will look into this a bit more this week when I'm able and then come back and award one of these answers as the correct one. Thank you for your help.
AHungerArtist
+1  A: 

Honestly, I'm not sure I understood all the constraints. Anyway, the recommended way to implement integration tests with Maven is to put them in a separate module (that's much easier, especially if the module under test also have unit tests) that depends on the war/ear under test and to:

  1. Start a container and deploy the war/ear during the pre-integration-test phase
  2. Have Maven run the tests during integration-test
  3. Stop the container during post-integration-test

For the steps #1 and #3, I personally use Cargo. For the step #2, using the Maven Failsafe Plugin is the preferred option (because it won't stop the build if a test fail). At least, this is what I use and I have used the resources below to build my setup.

An alternative approach would be to start/stop an embedded container from the tests. For example, this is doable with Jetty, see Embedding Jetty and Unit Test Servlets with Jetty.

Resources

Pascal Thivent
I will look into this a bit more this week when I'm able and then come back and award one of these answers as the correct one. Thank you for your help.
AHungerArtist