No Binaries in the Codebase

Binary data shouldn't be a part of the codebase. This is pretty well-known practice. But how to proceed when we do need binaries in our codebase, for instance as test data?


The solution is straight-forward: Pack binary resources into an artifact separated from the codebase in a repository.

A good practice is to use a special classifier for all the artifacts of this kind, for instance testdata.

After uploading the resources artifact into a repository (manually or via an API), we can use it as a dependency in our codebase (Maven):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>unpack-test-resources</id>
      <phase>process-test-resources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/test-classes</outputDirectory>
        <artifactItems>
          <artifactItem>
            <groupId>com.ttulka.samples.testdata</groupId>
            <artifactId>sample-test-resources</artifactId>
            <version>1.0.0</version>
            <classifier>testdata</classifier>
            <type>zip</type>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>

The Maven plugin will download and unpack the resources into the test classpath before the test are actually executed.

Binaries are then available in the codebase (Java):

this.getClass().getResource("/resource1.dat");

Another good practice is to create a domain-based resources structure inside the artifacts. Just put the resources into a sub-folder:

domainA.zip
┕ domainA/
  ┕ resource1.dat
  ┕ resource2.dat

This organization enables composition of resources in the integration testing.

The example code could be found in my GitHub.

Happy resourcing!