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.
The Maven configuration to create an archive of test helpers and attach it as an additional artifact looks like this:
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!