Over a million developers have joined DZone.

A Canonical Repository Test

DZone's Guide to

A Canonical Repository Test

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

There are only so many ways to test that your persistence layer is implemented correctly or that you’re using an ORM correctly. Here’s my canonical tests for a repository (Java-version):

import static org.fest.assertions.api.Assertions.*;
public class PersonRepositoryTest {
    private PersonRepository repository; // TODO < == you must initialize this
    public void shouldSaveAllProperties() {
        Person person = randomPerson();
        repository.save(person); // TODO: Make sure your repository flushes!
    public void shouldFindBName() {
        Person matching = randomPerson();
        Person nonMatching = randomPerson();
        matching.setName("A. Matching Person");
        nonMatching.setName("A. Random Person");

Very simple. The randomPerson test helper generates actually random people:

public class PersonTest {
    // ....
    public static Person randomPerson() {
        Person person = new Pesron();
        // TODO Initialize all properties
        return person;
    public static String randomName() {
        return RandomData.randomWord() + " " + RandomData.randomWord() + "son";
public class RandomData {
    public static String randomString() {
        return random("foo", "bar", "baz", "qux", "quux"); // TODO: Add more!
    public static <T> T random(T... options) {
        return options[random(options.length)];
    public static int random(int max) {
        return random.nextInt(max);
    private static Random random = new Random();

If your data has relationships with other entities, you may want to include those as well:

public class OrderRepositoryTest {
    private OrderRepository repository; // TODO < == you must initialize this
    private PersonRepository personRepository; // TODO <== you must initialize this
    private Person person = PersonTest.randomPerson();
    public void insertData() {
    public void shouldSaveAllProperties() {
        Order order = randomOrder(person);
        repository.save(order); // TODO: Make sure your repository flushes!

A simple and easy way to simplify your Repository testing.

(The tests use FEST assert 2 for the syntax. Look at FluentAssertions for a similar API in .NET)

(Yes, this is what some people would call an integration test. Personally, I can't be bothered with this sort of classifications)


Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Published at DZone with permission of Johannes Brodwall, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}