Play Framework 2.2.2 - Java -Testing Controllers With Mock Objects

932 views
Skip to first unread message

Damien Gallagher

unread,
Apr 7, 2014, 1:31:00 PM4/7/14
to play-fr...@googlegroups.com
Is anyone aware of any examples of testing a Java based Play Framework controller by setting mock objects? I am using Spring in my Play project so all my controller methods are not static.

Testing the tradional way, Play shows my controller as having static methods and I just cant see a way of how I can inject mocks into my object

Result result = callAction(
            controllers
.routes.ref.LoginController.authenticate(),
        fakeRequest
().withFormUrlEncodedBody(TestUtils.SUCCESSFUL_LOGIN_MAP)
   
);

I have a number of services that need to be called in the LoginController and I would like to set those up as mocks

Any help is greatly appreciated

Thanks Damien

Sean Scott

unread,
Apr 7, 2014, 2:02:08 PM4/7/14
to play-fr...@googlegroups.com
I am interested in what others do... what I did was Create an instance of my class manually and inject mock objects manually.  Then create a mock Context, set it as the current Context,  and just invoke your Controller instance directly:

( I use Mockito for mocks )

I am testing a Json REST service:

Invoke the controller



    Context.current.set(fakeContext(Json.toJson(


        new Object() {


          public String foo = "goodUser";


          public String password = "goodPassword";


        }).toString()));


    result = auth.authenticate();


    assertThat(status(result)).isEqualTo(Status.BAD_REQUEST);




Make the fake context



  public static Context fakeContext(String body)


  {


    Request request = mock(Request.class);


    RequestBody requestBody = mock(RequestBody.class);


    when( request.body() ).thenReturn(requestBody);


   


    if(StringUtils.isNotEmpty(body))


    {


      when( request.method() ).thenReturn("POST");


      when( requestBody.asText() ).thenReturn(body);


     


      try


      {


        when( requestBody.asJson() ).thenReturn(new ObjectMapper().readTree(body));


      }


      catch (IOException e)


      {


        throw new RuntimeException(e);


      }


     


    }


   


    when(request.host()).thenReturn("localhost");


   


    RequestHeader header = mock(RequestHeader.class);


    when( header.host()).thenReturn("localhost");


   


    Context ctx = mock(Context.class);


    when(ctx._requestHeader()).thenReturn(header);


    when(ctx.request()).thenReturn(request);


    when(ctx.flash()).thenReturn(new Flash(new HashMap<String,String>()));


    when(ctx.lang()).thenReturn(Lang.forCode("en"));


   


   


   


    return ctx;


  }

Alberto Souza

unread,
Apr 7, 2014, 2:09:04 PM4/7/14
to play-fr...@googlegroups.com

Damien Gallagher

unread,
Apr 9, 2014, 4:06:49 AM4/9/14
to play-fr...@googlegroups.com
Thanks guys

I am able to use PowerMock to mock the tests but not with the standard items used the play framework

Ill keep playing with it
Reply all
Reply to author
Forward
0 new messages