views:

24

answers:

1

So I have some classes that rely on a jar file that has native methods in them. I am running into issues when mocking the objects in this jar file...so I have found a solution that works.

Using forkedmode pertest seems to fix this issue. However, there are 5 files affected by needing to be run in forkedmode...there are 130 other tests that do not need forking, and the build time with cobertura and everything is VERY slow as it is forking for every test in that pom...

So my question is...is there a way to specify which classes you want to run in forkedmode and run everything else normally?

+2  A: 

is there a way to specify which classes you want to run in forkedmode and run everything else normally?

You can do this by specifying two <execution> elements with specific <configuration>: a default one for most tests (excluding those that need to be forked) with the forkMode set to once and a special one for the special tests (including only the special one) where the forkMode set to always.

Here is a pom snippet showing how to do this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <!-- Lock down plugin version for build reproducibility -->
  <version>2.6</version>
  <executions>
    <execution>
      <id>default-test</id><!-- here we configure the default execution -->
      <configuration>
        <forkMode>once</forkMode><!-- this is the default, can be omitted -->
        <excludes>
          <exclude>**/somepackage/*Test.java</exclude>
        </excludes>
      </configuration>
    </execution>
    <execution>
      <id>special-test</id><!-- and here we configure the special execution -->
      <phase>test</phase>
      <goals>
        <goal>test</goal>
      </goals>
      <configuration>
        <forkMode>always</forkMode>
        <includes>
          <include>**/somepackage/*Test.java</include>
        </includes>
      </configuration>
    </execution>
  </executions>
</plugin>

See also

Pascal Thivent
yup, that's the way to go
seanizer
Thanks, worked perfectly! I'm still relatively new to Maven and editing pom files.
bmucklow
@bmucklow You're welcome (and while maven can handle your use case very nicely, it is already not that trivial IMHO). Glad it was helpful.
Pascal Thivent