mocking - Play Framework 2.2.2 - Java -Testing Controllers With Mock Objects -
is aware of examples of testing java based play framework controller setting mock objects? using spring in play project controller methods not static.
testing tradional way, play shows controller having static methods , cant see way of how can inject mocks object
result result = callaction( controllers.routes.ref.logincontroller.authenticate(), fakerequest().withformurlencodedbody(testutils.successful_login_map) );
i have number of services need called in logincontroller , set mocks
any appreciated
thanks damien
i looking solution of same problem. far best result able achieve this:
public class myobjectcontrollertest{ private final myobjectdao dao = mock(myobjectdao.class); private final myobjectcontroller controller = new myobjectcontroller(dao); public static fakeapplication fakeapplication; @beforeclass public static void startapp() { fakeapplication = helpers.fakeapplication(); helpers.start(fakeapplication); } @afterclass public static void stopapp() { helpers.stop(fakeapplication); } @test(expected = notfoundexception.class) public void testfailwithunknownmyobjectkey() throws throwable { when(dao.getbykey(any(uuid.class), any(uuid.class), any(boolean.class))).thenreturn(null); controller.get(cassandrauuids.timebased()); } @test public void testgetsuccess(){ myobject deletedobject = myobjecttestgenerator.generatemyobject(); deletedobject.setdeleted(true); when(dao.getbykey(any(uuid.class), any(uuid.class), any(boolean.class))).thenreturn(deletedobject); try { result result = controller.get(cassandrauuids.timebased()); assertthat(status(result)).isequalto(http.status.gone); assertthat(contenttype(result)).isequalto(http.mimetypes.json); assertthat(contentasstring(result)).isequalto(errormsg.object_deleted.tojson().tostring()); } catch (myobjectsexception e) { e.printstacktrace(); fail("failed send myobject.get request."); } } }
what here instantiate instance of controller class , pass mocked dao instance. please note don't use static controller methods in code well.
one issue workaround found far action
(i have custom one) not working. action
can (and must) tested separately.
Comments
Post a Comment