Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Thinking Inside a Bigger Box

Welcome to Johannes Brodwall's blog. I use this space to work on articles mostly about software development, with a focus on Java, SOA, and Agile software development. Many of the articles you will find here are not much more than drafts, and I certainly appreciate input on how to make them better.

If you wonder about the title of this blog, Thinking Outside the Box may answer your questions.

I work as the lead software architect of BBS, the company that handles interbank services in Norway. In my copious free time, I develop software and consult companies in development practices and architecture. For more about the services I can offer, please see my resume.


A canonical Repository test

 

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
 
@Test
public void shouldSaveAllProperties() {
Person person = randomPerson();
repository.save(person); // TODO: Make sure your repository flushes!
assertThat(repository.find(person.getId())
.isNotSameAs(person)
.isEqualTo(person)
.isEqualsToByComparingFields(person);
}
 
@Test
public void shouldFindByCaseInsensitiveSubstringOfName() {
Person matching = randomPerson();
Person nonMatching = randomPerson();
matching.setName("A. Matching Person");
nonMatching.setName("A. Random Person");
repository.save(matching);
repository.save(nonMatching);
assertThat(repository.findByNameLike("MATCH"))
.contains(matching)
.doesNotContain(nonMatching);
}
}

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

public class PersonTest {
// ....
public static Person randomPerson() {
Person person = new Pesron();
person.setName(randomName());
// 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();
 
@Before
public void insertData() {
personRepository.save(person);
}
 
@Test
public void shouldSaveAllProperties() {
Order order = randomOrder(person);
repository.save(order); // TODO: Make sure your repository flushes!
assertThat(repository.find(order.getId())
.isNotSameAs(order)
.isEqualTo(order)
.isEqualsToByComparingFields(order);
}

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)