Constructing complex objects for tests can be simplified by builders. If these builders are not part of the API, they can be added to the test classes and exported via the Maven Jar Plugin.

 

We cannot add these test helper classes to another project, since we want to use them in the project’s unit tests, too. Therefore we started to use the artificial ‘top-level’ domain help in our projects to group all our test helpers to be exported and used by other projects for writing unit tests.

  help.de.smartics.something.XBuilder
  help.de.smartics.something.YBuilder
  ...

The Maven configuration to create an archive of test helpers and attach it as an additional artifact looks like this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <executions>
    <execution>
      <id>testhelper</id>
      <goals>
        <goal>jar</goal>
      </goals>
      <configuration>
        <classifier>testhelpers</classifier>
        <classesDirectory>
          ${project.build.testOutputDirectory}
        </classesDirectory>
        <includes>
          <include>help/**</include>
        </includes>
      </configuration>
    </execution>
  </executions>
</plugin>

We add the classifier testhelpers to identify the artifact as containing our test helpers. The classifier tests is still used, if we export our test case classes.

For more information about writing and organizing tests, please have a look at Growing Object-Oriented Software Guided by Tests by Steve Freeman and Nat Pryce!

Please note

 

Due to MNG-3559 you will encounter difficulties on multi-module builds when creating a site. The workaround is to run phase install before phase site (or try to avoid modules, see Nicholas Williams’ comment on MNG-3559 for details).

mvn install site


Link

Link

Posts

Tagcloud

Loading tagcloud ...