Fakedata

In any given test there are often quite a few variables involved, to initialize the state of the objects, or to act as arguments for invoking a method, or when asserting on post-conditions. Sometimes those values are important (eg verifying that an Order's state went from PENDING to SHIPPED, say), but often they aren’t (a customer’s name, for example). Nevertheless all this data may need to be set up, especially in integration tests.

We want our tests to be easily understood, and we want the reader’s eye to be drawn to the values that are significant and ignore those that are not.

One way to do this is to use random (or fake) values for any insignificant data. This in effect tells the reader that "any value will do". Moreover, if it turns out that any data won’t do, and that there’s some behaviour that is sensitive to the value, then the test will start to flicker, passing and then failing depending on inputs. This is A Good Thing™.

To enable this the fakedata library provides the FakeDataService whose purpose is to act as a supplier of these random values, for numerous different data types, as well as names and "lorem" text.

  • If using within an integration test, the FakeDataService can simply be injected.

  • If using within a unit test, then be aware that to be instantiated the service requires a ClockService and a RepositoryService. The former is used obtain the current time (to return random date/times before, around or after that time), while the latter is used to look up random instances of entities (through the collections() method).

We recommend using fake data in both unit and integration tests as a best practice.

Maven Configuration

Dependency Management

If your application inherits from the Apache Causeway starter app (org.apache.causeway.app:causeway-app-starter-parent) then that will define the version automatically:

pom.xml
<parent>
    <groupId>org.apache.causeway.app</groupId>
    <artifactId>causeway-app-starter-parent</artifactId>
    <version>2.0.0</version>
    <relativePath/>
</parent>

Alternatively, import the core BOM. This is usually done in the top-level parent pom of your application:

pom.xml
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.causeway.core</groupId>
            <artifactId>causeway-core</artifactId>
            <version>2.0.0</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

In addition, add an entry for the BOM of all the testing support libraries:

pom.xml
<dependencyManagement>
    <dependencies>
        <dependency>
        	<groupId>org.apache.causeway.testing</groupId>
	        <artifactId>causeway-testing</artifactId>
            <scope>import</scope>
            <type>pom</type>
            <version>2.0.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>

Dependencies

In the domain module(s) of your application, add the following dependency:

pom.xml
<dependencies>
    <dependency>
        <groupId>org.apache.causeway.testing</groupId>
        <artifactId>causeway-testing-fakedata-applib</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Update AppManifest

In your application’s AppManifest (top-level Spring @Configuration used to bootstrap the app), import the CausewayModuleTestingFakeDataApplib module:

AppManifest.java
@Configuration
@Import({
        ...
        CausewayModuleTestingFakeDataApplib.class,
        ...
})
public class AppManifest {
}

API

The library provides a single service, FakeDataService. This provides numerous methods, each for a specific data type, providing an object that is able to provide random instances of that data type.

Dates and times

Note: these require the ClockService to be injected through the constructor):

Domain-specific

In addition, the full API of Java Faker is also accessible.