Skip to content

Instantly share code, notes, and snippets.

@bverbeken
Created October 19, 2012 11:31
Show Gist options
  • Save bverbeken/3917715 to your computer and use it in GitHub Desktop.
Save bverbeken/3917715 to your computer and use it in GitHub Desktop.
Custom JUnit runner for Playframework (2.0) apps to eliminate boilerplate code
@RunWith(PlayJUnitRunner.class)
public class AfterOrganisationTest {
@Test
public void canBePersisted() {
new Organisation("org1").save();
Organisation reloadedOrg = Ebean.find(Organisation.class).findUnique();
assertThat(reloadedOrg.name).isEqualTo("org1");
}
}
import static play.test.Helpers.*;
public class BeforeOrganisationTest {
@Test
public void aTestInAnApplication() {
running(fakeApplication(inMemoryDatabase()), new Runnable() {
@Override
public void run() {
new Organisation("org1").save();
Organisation reloadedOrg = Ebean.find(Organisation.class).findUnique();
assertThat(reloadedOrg.name).isEqualTo("org1");
}
});
}
}
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
import play.test.FakeApplication;
import static play.test.Helpers.*;
public class PlayJUnitRunner extends BlockJUnit4ClassRunner {
public PlayJUnitRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
public void run(final RunNotifier notifier) {
FakeApplication app = fakeApplication(inMemoryDatabase());
start(app);
super.run(notifier);
stop(app);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment