Skip to content

Instantly share code, notes, and snippets.

@schaloner
Last active January 3, 2016 11:09
Show Gist options
  • Save schaloner/8454626 to your computer and use it in GitHub Desktop.
Save schaloner/8454626 to your computer and use it in GitHub Desktop.
Load externally-defined data into an in-memory database for Play 2 testing.
package controllers;
import models.Item;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
public class Application extends Controller
{
public static Result index()
{
return ok(Json.toJson(Item.getAll()));
}
}
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "dataload"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
javaCore,
javaJdbc,
javaEbean,
"org.dbunit" % "dbunit" % "2.4.9" % "test" exclude("junit", "junit"),
"com.jayway.restassured" % "rest-assured" % "2.2.0" % "test"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
)
}
package controllers;
import com.jayway.restassured.RestAssured;
import models.Item;
import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.junit.Assert;
import org.junit.Test;
import play.Application;
import play.Configuration;
import play.GlobalSettings;
import play.test.FakeApplication;
import static play.test.Helpers.*;
public class DataLoadingTest
{
@Test
public void testGetItems()
{
FakeApplication app = fakeApplication(new TestData("/items.xml"));
RestAssured.port = 3333;
running(testServer(3333, app), new Runnable()
{
@Override
public void run()
{
Item[] items = RestAssured.expect()
.statusCode(200)
.when()
.get("/")
.andReturn()
.body()
.as(Item[].class);
Assert.assertEquals(4, items.length);
Assert.assertEquals("Fee", items[0].name);
Assert.assertEquals("Fi", items[1].name);
Assert.assertEquals("Fo", items[2].name);
Assert.assertEquals("Fum", items[3].name);
}
});
}
@Test
public void testGetItems_emptyDb()
{
RestAssured.port = 3333;
running(testServer(3333), new Runnable()
{
@Override
public void run()
{
Item[] items = RestAssured.expect()
.statusCode(200)
.when()
.get("/")
.andReturn()
.body()
.as(Item[].class);
Assert.assertEquals(0, items.length);
}
});
}
private class TestData extends GlobalSettings
{
private final String dataFile;
private TestData(String dataFile) {
this.dataFile = dataFile;
}
@Override
public void onStart(Application application)
{
try
{
final Configuration configuration = application.configuration();
final FlatXmlDataSet xmlDataSet = new FlatXmlDataSetBuilder().build(TestData.class.getResourceAsStream(dataFile));
IDatabaseTester databaseTester = new JdbcDatabaseTester(
configuration.getString("db.default.driver"),
configuration.getString("db.default.url"),
configuration.getString("db.default.user"),
configuration.getString("db.default.password"));
databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
databaseTester.setDataSet(xmlDataSet);
databaseTester.onSetup();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
package models;
import play.db.ebean.Model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.List;
@Entity
public class Item extends Model
{
private static final Finder<Long, Item> FIND = new Finder<>(Long.class, Item.class);
@Id
public Long id;
@Column(nullable = false)
public String name;
public static List<Item> getAll()
{
return FIND.all();
}
}
<dataset>
<item id="1" name="Fee"/>
<item id="2" name="Fi"/>
<item id="3" name="Fo"/>
<item id="4" name="Fum"/>
</dataset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment