Java 如何在Play 2.1.0中测试XML结果

Java 如何在Play 2.1.0中测试XML结果,java,playframework,playframework-2.1,Java,Playframework,Playframework 2.1,我正在尝试为Play2.1.0的控制器方法编写一个测试用例。该方法接收一个XML对象,执行一些操作,并根据XML返回OK或BAD_请求,其中有一部分: @BodyParser.Of(BodyParser.Xml.class) public static Result save() { Document dom = request().body().asXml(); System.out.println(request().body()); if (dom == null)

我正在尝试为Play2.1.0的控制器方法编写一个测试用例。该方法接收一个XML对象,执行一些操作,并根据XML返回OK或BAD_请求,其中有一部分:

@BodyParser.Of(BodyParser.Xml.class)
public static Result save() {
    Document dom = request().body().asXml();
    System.out.println(request().body());
    if (dom == null) {
        return badRequest(error.render("Se esperaba Xml"));
    } else {
        ...
当我尝试用curl测试它时,效果很好:

$ curl --header "Content-type: text/xml" --request POST --data '<gloo>Guillaume</gloo>' -verbose http://localhost:9000/api/xml/
* About to connect() to localhost port 9000 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9000 (#0)
> POST /api/xml/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:9000
> Accept: */*
> Referer: rbose
> Content-type: text/xml
> Content-Length: 22
> 
* upload completely sent off: 22 out of 22 bytes
< HTTP/1.1 200 OK
< Content-Type: text/xml; charset=utf-8
< Content-Length: 89
< 


* Connection #0 to host localhost left intact
<?xml version="1.0" encoding="utf-8"?><gloo>dd8d191c-8a27-4dad-a624-1c5a6a5d9d03</gloo>
$curl--header“Content-type:text/xml”--请求发布--data'Guillaume'-详细http://localhost:9000/api/xml/
*即将连接()到本地主机端口9000(#0)
*正在尝试127.0.0.1。。。
*已连接到本地主机(127.0.0.1)端口9000(#0)
>POST/api/xml/HTTP/1.1
>用户代理:curl/7.29.0
>主机:localhost:9000
>接受:*/*
>推荐人:卡波糖
>内容类型:text/xml
>内容长度:22
> 
*上传已完全发送:22个字节中的22个
但是当我尝试测试时,它看起来像是我在向控制器传递一个错误的XML。这是我的测试用例:

@Test
public void saveActionRespondsOkOnValidContent() {
    running(fakeApplication(), new Runnable() {
        public void run() {
            Result result = route(fakeRequest(POST, "/api/xml/")
                    .withHeader(CONTENT_TYPE, "text/xml").withTextBody(
                            "<gloo>Probando el API xml</gloo>"));
            System.out.println(contentAsString(result));
            assertThat(status(result)).isEqualTo(OK);
            assertThat(contentType(result)).isEqualTo("text/xml");
            assertThat(contentAsString(result)).isNotEmpty();
        }
    });
}
@测试
public void saveActionRespondsOkOnValidContent()的{
正在运行(fakeApplication(),new Runnable()){
公开募捐{
结果=路线(fakeRequest(POST,“/api/xml/”)
.withHeader(内容类型,“text/xml”).withTextBody(
"Probando el API xml),;
System.out.println(contentAsString(result));
资产(状态(结果)).isEqualTo(正常);
assertThat(contentType(result)).isEqualTo(“text/xml”);
断言(contentAsString(result)).isNotEmpty();
}
});
}
这就是我运行“播放测试”时得到的结果:

。。。
DefaultRequestBody(无,无,无,无,无,无,真)
Se esperaba Xml
[错误]测试api.XmlApiTests.saveActionRespondsOkOnValidContent失败:应为:但为:
...
第一行由控制器跟踪,表示没有XML。 第二个由测试用例跟踪,表示收到的错误消息。 第三个是我的测试用例失败:我在等待OK时收到了一个错误的请求

所以我的问题(最后)是:
我如何编写一个向我的方法发送有效XML的测试用例?

FakeRequest
中似乎有一个方法
带有xmlbody
。使用该方法而不是使用text body
应该会有所帮助

另一个尝试(我自己没有测试过)是将
内容类型
更改为
应用程序/xml

...
DefaultRequestBody(None,None,None,None,None,None,true)
<?xml version="1.0" encoding="utf-8"?><gloo>Se esperaba Xml</gloo>
[error] Test api.XmlApiTests.saveActionRespondsOkOnValidContent failed: expected:<[2]00> but was:<[4]00>
...