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
We recommend using fake data in both unit and integration tests as a best practice.
If your application inherits from the Apache Isis starter app (
org.apache.isis.app:isis-app-starter-parent) then that will define the version automatically:
<parent> <groupId>org.apache.isis.app</groupId> <artifactId>isis-app-starter-parent</artifactId> <version>2.0.0-M9</version> <relativePath/> </parent>
Alternatively, import the core BOM. This is usually done in the top-level parent pom of your application:
<dependencyManagement> <dependencies> <dependency> <groupId>org.apache.isis.core</groupId> <artifactId>isis-core</artifactId> <version>2.0.0-M9</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement>
In addition, add a section for the BOM of all the testing support libraries:
<dependencyManagement> <dependencies> <dependency> <groupId>org.apache.isis.testing</groupId> <artifactId>isis-testing</artifactId> <scope>import</scope> <type>pom</type> <version>2.0.0-M9</version> </dependency> </dependencies> </dependencyManagement>
In the domain module(s) of your application, add the following dependency:
<dependencies> <dependency> <groupId>org.apache.isis.testing</groupId> <artifactId>isis-testing-fakedata-applib</artifactId> <scope>test</scope> </dependency> </dependencies>
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):
In addition, the full API of Java Faker is also accessible.