我是Play Framework的新手,我是uisng 2.6版示例项目 在测试文件夹里面,我写了一个测试方法,如下所示
测试外部网络服务http://techpaisa.com/supres/acc/
package it;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static play.test.Helpers.GET;
import static play.test.Helpers.contentAsString;
import static play.test.Helpers.route;
import org.junit.Test;
import play.Application;
import play.inject.guice.GuiceApplicationBuilder;
import play.mvc.Http;
import play.mvc.Result;
import play.test.WithApplication;
import v1.post.PostData;
import v1.post.PostRepository;
public class IntegrationTest extends WithApplication {
@Override
protected Application provideApplication() {
return new GuiceApplicationBuilder().build();
}
@Test
public void testList() {
PostRepository repository = app.injector().instanceOf(PostRepository.class);
repository.create(new PostData("title", "body"));
Http.RequestBuilder request = new Http.RequestBuilder().method(GET).uri("http://techpaisa.com/supres/acc/");
System.out.println("THE REQIEST IS "+request.body().toString());
Result result = route(app, request);
System.out.println("THE RESULT IS "+result.status());
}
}
这是我看到的输出
[success] Total time: 81 s, completed 10 Dec, 2017 9:47:23 PM
[play-java-rest-api-example] $ test
[info] Compiling 1 Java source to E:\minetest\play-java-rest-api-example\target\scala-2.12\test-classes ...
[info] Done compiling.
[info] Test run started
[info] Test it.IntegrationTest.testList started
[info] application - Creating Pool for datasource 'default'
[info] p.a.d.HikariCPConnectionPool - datasource [default] bound to JNDI as DefaultDS
[info] a.e.s.Slf4jLogger - Slf4jLogger started
THE REQIEST IS RequestBody of null
THE RESULT IS 404
[info] application - Shutting down connection pool.
[info] Test run finished: 0 failed, 0 ignored, 1 total, 9.37s
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 14 s, completed 10 Dec, 2017 9:48:14 PM
有人可以告诉我为什么请求为空
我甚至做得很简单,但仍然是同样的错误
@Test
public void testBadRoute() {
RequestBuilder request = Helpers.fakeRequest()
.method(GET)
.uri("http://techpaisa.com/supres/acc/");
System.out.println("body"+request.body());
Result result = route(app, request);
System.out.println(result.status());
}
答案 0 :(得分:2)
您的请求不是null
,而是正文:request.body()
。在GET
请求的情况下,拥有一个空体是正常的。