@Test
public void findById() {
running(fakeApplication(inMemoryDatabase("test")), new Runnable() {
public void run() {
Computer macintosh = Computer.findById(21l);
assertThat(macintosh.name).isEqualTo("Macintosh");
assertThat(formatted(macintosh.introduced)).isEqualTo("1984-01-24");
}
});
}
It's much cleaner to do that with with the Helpers class and JUnit setup and teardowns (i.e., @Before and @After)
public class ApplicationTest {
private FakeApplication fa;
private FakeApplication provideFakeApplication()
{
return Helpers.fakeApplication(Helpers.inMemoryDatabase());
}
@Before
public void startapp()
{
fa = provideFakeApplication();
Helpers.start(fa);
}
@After
public void stopapp()
{
Helpers.stop(fa);
}
@Test
public void findByID() {
/* Runs in a FakeApplication context */
Computer macintosh = Computer.findById(21l);
assertThat(macintosh.name).isEqualTo("Macintosh");
assertThat(formatted(macintosh.introduced)).isEqualTo("1984-01-24");
}
...
}
This way you take advantage of well-understood contextualization through setup and teardown methods while keeping the test itself focused on what it is testing. Besides, using the wrapper can't even be justified for one-off tests except for the fact that you might forget to call Helpers.stop.
~~~~