views:

47

answers:

3

I am trying to get integration tests going with Spring, Maven and Jetty. I have got a Jetty server to start up running my app before the integration tests start using the Maven Jetty plugin and a run-exploded pre-integration-test goal. This works well for testing using net.sourceforge.jwebunit.junit.WebTestCase.

What I need to do now is have "extra" beans loaded into the app when it is started for the integration tests. This would be easy if my test classes and resources could be deployed with the app somehow. Anyone know an easy way to do this?

I want to be able to "mock out" some stuff (e.g. sending email) and write integration tests that cannot easily be done purely through the "front end".

+1  A: 

You could define an "integration" profile in your pom-file which is triggered by running mvn with the -Pintegration flag. Then you use a different applicationContext.xml defining the extra beans from an environment specific directory, say, src/main/external-resources/integration.

<profile>
  <id>integration</id>
  <properties>
    <env>integration</env>
  </properties>
  <build>
    <resources>
      <resource>
        <directory>src/main/external-resources/${env}</directory>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    ...
Jeroen Rosenberg
+4  A: 

What I need to do now is have "extra" beans loaded into the app when it is started for the integration tests. This would be easy if my test classes and resources could be deployed with the app somehow. Anyone know an easy way to do this?

I would create a separate integration tests module, put the extra tests classes and configuration files in the source tree and use Overlays to create a "test WAR" including these files. See JSFUnit With Maven for a similar approach.

Pascal Thivent
+2  A: 

I found an easy solution on this thread. The Jetty plugin supports adding extra stuff to the classpath of the web app. This is perfect as I just have to annotate my extra test beans with @Component and Spring loads them into the app.

<webAppConfig>
  <contextPath>/${project.name}</contextPath>
  <extraClasspath>${project.build.testOutputDirectory}</extraClasspath>
</webAppConfig>
David Tinker
Indeed, that's another option :)
Pascal Thivent