Tuesday 15 September 2015

playframework 2.0 - Integration test Service layer Play Framework -



playframework 2.0 - Integration test Service layer Play Framework -

im using play framework 2.0 - given have paymentservice access databases.

today test first firing test server:

// set , start false web application fakeapplication fakeapp = fakeapplication(inmemorydatabase()); start(fakeapp); // jpaplugin through false app, , start option<jpaplugin> jpaplugin = fakeapp.getwrappedapplication().plugin(jpaplugin.class); jpaplugin.get().onstart(); // through jpa plugin, access entity manager final entitymanager manager = jpaplugin.get().em("default"); // , bind in thread local jpa.bindforcurrentthread(manager); jpa.em().gettransaction().begin();

after done can start accessing database, insert pre state, execute methods on service, , assert (db) post state

but doesnt sense right fire whole web server (even if false server) when test service layer access entity manager.

is there smarter way integration test service layer? coming spring world, assume should possible create entity manager manually instead of having play server doing us.

any help / tips / directions appreciated.

i suggest using testserver class helpers class. can utilize run in memory instance of play junit test , run false requests against instance. instance , plugins initialized application.conf.

minimal configuration:

app = fakeapplication(inmemorydatabase()); server = helpers.testserver(9009, app); webdriver = play.api.test.webdriverfactory.apply(htmlunit); helpers.start(server); browser = helpers.testbrowser(webdriver);

actual test:

result result = helpers.route(helpers.fakerequest(get, "/data...")); assertnotnull(result);

and remember clean up:

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

playframework-2.0

No comments:

Post a Comment