2013-12-20 1 views
1

Play 2.1.0의 컨트롤러 메서드에 대한 테스트 사례를 작성하려고합니다. 나는 컬로 테스트하려고 잘 작동 할 때Play 2.1.0에서 XML 결과를 테스트하는 방법

@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 { 
     ... 

: : 방법은 그것의 일부가, XML 객체를 수신 몇 가지 물건을 수행하고 OK 또는 XML에 따라 BAD_REQUEST을 반환

$ 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> 

하지만 테스트하려고하면 잘못된 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(); 
     } 
    }); 
} 

그리고는 "플레이 테스트"를 실행할 때이 내가 가진 무엇을 : 더 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> 
... 

첫 번째 줄은 컨트롤러에 의해 추적된다. 두 번째 것은 테스트 케이스에 의해 추적되고 수신 된 오류 메시지입니다. 세 번째 테스트 케이스가 실패합니다. OK를 기다리고있을 때 BAD_REQUEST가 표시됩니다.

내 질문 (마침내) : 유효한 XML을 내 메서드로 보내는 테스트 사례를 작성하려면 어떻게해야합니까?

답변

0

withXmlBodyFakeRequest에 있으며 here으로 표시되어 있습니다. withTextBody 대신 해당 방법을 사용하면 도움이됩니다.

또 다른 것은 변경하는 것 (I이 나 자신을 테스트하지 않은) 시도 Content-Typeapplication/xml

관련 문제