Invalid JSON in Playframework 2.1 Test POST Request

微笑、不失礼 提交于 2020-01-04 02:56:07

问题


I am trying to test a POST route (on Playframework 2.1/ Java) and keep getting Bad Request - Invalid JSON response.

Please let me know what I am doing wrong. My Test code is below.

 running(fakeApplication(), new Runnable() {
                        public void run() {

            Map map = new HashMap();
            map.put("key1", "val1");
            map.put("key2", 2);
            map.put("key3", true);
            JsonNode df= Json.toJson(map);

            Result result = route
                    (fakeRequest(POST, "/item").withHeader("Content-Type", "application/json").withSession(AccountTest.USER_KEY, "some key")
                            .withSession(AccountTest.PROVIDER_KEY, "facebook").withJsonBody(df));

            Logger.debug("result is "  + contentAsString(result));
            System.out.println("result is "  + contentAsString(result));

                assertThat(result).isNotNull();

//                assertThat(result).isNotNull();
//                assertThat(result).isEqualTo("hello");
            }

        });

回答1:


This a documented bug in Play 2.1 (Java). Please refer to the bug here and the git-pull request https://play.lighthouseapp.com/projects/82401/tickets/984-adding-json-body-to-play-21-java-returns-invalid-json-message#ticket-984-8



来源:https://stackoverflow.com/questions/14804162/invalid-json-in-playframework-2-1-test-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!