I wanted to test one of my POST methods in my controller, so I wrote something like this:
@Test public void shouldSaveNewCollectionToDatabase(){ String body = "{\"name\":\"collectionName\", \"owner\": {}}"; JsonNode json = Json.parse(body); FakeRequest request = new FakeRequest(POST, "/rest/collections/add").withJsonBody(json); Result result = callAction(controllers.routes.ref.SetsAndCollections.postCollection(), request); verify(questionSetCollectionDAO).save(any(QuestionSetCollection.class)); }
The fact is that this test fails because the controller method is not called at all, so my questionSetCollectionDAO methods are not called.
Event
I puts some print at the beginning of the method:
@BodyParser.Of(Json.class) @play.db.jpa.Transactional public static Result postCollection(){ System.out.println("I am here"); ...
and I do not see the console output.
If this is not the case, how can I call controller methods with fake requests, how can I do this?
I read about fakeApplication , but do I have any other way to do simple testing of the POST controller methods?
source share