在最新的文档中,以这个模板测试为例。
@Test
public void renderTemplate() {
Content html = views.html.index.render("Coco");
assertThat(contentType(html)).isEqualTo("text/html");
assertThat(contentAsString(html)).contains("Coco");
}但我该怎么做呢?我自己尝试过,在一个假服务器上的run()方法中,一个真正的服务器,在一个实际运行的服务器上,我总是得到这个错误。
[error] Test ApplicationTest.testInServer failed: java.lang.RuntimeException: There is no HTTP Context available from here.文档中实际上有两页关于测试的内容,我不知道如何实际运行这些测试。是否有一个示例类在任何地方不使用不推荐的方法(自Play 1以来情况已经发生了变化,并且大多数事情不再工作了)。
发布于 2014-04-01 20:26:48
为什么不使用硒呢?
请参阅帮助的最后一部分:http://www.playframework.com/documentation/2.0/JavaFunctionalTest
发布于 2014-04-04 13:05:25
您需要首先设置Http上下文。
Mockito示例:
@Test
public void indexTest() {
//setup HTTP Context
Http.Context context = Mockito.mock(Http.Context.class);
//mocking flash session, request, etc... as required
Http.Flash flash = Mockito.mock(Http.Flash.class);
when(context.flash()).thenReturn(flash);
Http.Context.current.set(context);
//run your test
Content html = views.html.index.render("Coco");
assertThat(contentType(html)).isEqualTo("text/html");
assertThat(contentAsString(html)).contains("Coco");
}发布于 2014-04-01 19:23:19
从命令行运行“播放测试”。它将运行测试文件夹中的所有测试。
https://stackoverflow.com/questions/22791538
复制相似问题