Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit assert against play.mvc.Result content?

How does one assert against content within the body of a Play! JSON action Result value? I'm returning a new (or even null) JSON ObjectNode from an action and attempting to assert against it within the result from a unit test, but don't see the value (null or otherwise) in the response.wrappedResult.body.

For example, in my action, if I return a null JSON Object Node,

ObjectNode response = null;
return ok(response);

or if I return a test value,

ObjectNode response = Json.newObject();
response.put("testKey", "testValue");
return ok(response);

and then I write a test against the result,

@Test
public void MyTest() {
    Result result = MyController.myAction();
    // assert against null or specified values here
}

and I then explore the entire object graph,

result.wrappedResult.body

I don't see anything that looks like my specified results to assert against.

Any ideas?

Thanks!

like image 540
user364825 Avatar asked Mar 14 '26 21:03

user364825


1 Answers

Take a look at Testing your controllers in the Play docs on Java testing for an example of how to correctly call your actions and test Result content.

@Test
public void callIndex() {
    Result result = callAction(
      controllers.routes.ref.Application.index("Kiki")
    );
    assertThat(status(result)).isEqualTo(OK);
    assertThat(contentType(result)).isEqualTo("text/html");
    assertThat(charset(result)).isEqualTo("utf-8");
    assertThat(contentAsString(result)).contains("Hello Kiki");
}

Play does some clever stuff when it calls actions so you need to wrap your call in callAction. You won't get the same result if you call the action method directly.

like image 162
Rich Dougherty Avatar answered Mar 17 '26 15:03

Rich Dougherty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!