Playframework 2.0 集成测试服务层播放框架

Playframework 2.0 集成测试服务层播放框架,playframework-2.0,Playframework 2.0,我使用PlayFramework2.0——假设我有一个访问数据库的PaymentService 今天,我首先启动测试服务器来测试这一点: // set up and start the fake web application FakeApplication fakeApp = fakeApplication(inMemoryDatabase()); start(fakeApp); // get the JPAPlugin through the fake app, and start it O

我使用PlayFramework2.0——假设我有一个访问数据库的PaymentService

今天,我首先启动测试服务器来测试这一点:

// set up and start the fake web application
FakeApplication fakeApp = fakeApplication(inMemoryDatabase());
start(fakeApp);
// get the JPAPlugin through the fake app, and start it
Option<JPAPlugin> jpaPlugin = fakeApp.getWrappedApplication().plugin(JPAPlugin.class);
jpaPlugin.get().onStart();
// then through the JPA plugin, get access to the entity manager
final EntityManager manager = jpaPlugin.get().em("default");
// and bind it in the thread local
JPA.bindForCurrentThread(manager);
JPA.em().getTransaction().begin();
//设置并启动假web应用程序
FakeApplication fakeApp=FakeApplication(inMemoryDatabase());
启动(fakeApp);
//让JPAPlugin通过假应用程序,并启动它
选项jpaPlugin=fakeApp.getWrappedApplication().plugin(jpaPlugin.class);
jpaPlugin.get().onStart();
//然后通过JPA插件访问实体管理器
final EntityManager manager=jpaPlugin.get().em(“默认”);
//并将其绑定到本地线程中
JPA.bindForCurrentThread(管理器);
JPA.em().getTransaction().begin();
完成后,我可以开始访问数据库,插入pre状态,在服务上执行方法,并断言(DB)post状态

但是,当我测试服务层只是为了访问实体管理器时,启动整个web服务器(即使它是一个假的服务器)是不对的

有没有更智能的方法来集成测试服务层? 来自Spring世界,我认为应该可以手动创建实体管理器,而不是让Play服务器为我们创建实体管理器

任何帮助/提示/指导都将不胜感激。

我建议使用课堂和课堂。您可以使用它在junit测试的play内存实例中运行,然后对该实例运行伪请求。实例和插件将使用application.conf进行初始化

最低配置:

app = fakeApplication(inMemoryDatabase());
server = Helpers.testServer(9009, app);
webDriver = play.api.test.WebDriverFactory.apply(HTMLUNIT);
Helpers.start(server);
browser = Helpers.testBrowser(webDriver);
实际测试:

Result result = Helpers.route(Helpers.fakeRequest(GET, "/data..."));
assertNotNull(result);
记得清理一下:

browser.quit();
Helpers.stop(server);

我很高兴能帮上忙。