【问题标题】:PlayFramework 2 template testingPlayFramework 2 模板测试
【发布时间】:2014-04-04 21:05:25
【问题描述】:

在最新的documentation中,以这个模板测试为例。

@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 以来情况发生了变化,大多数事情不再起作用)。

【问题讨论】:

    标签: java junit playframework playframework-2.0


    【解决方案1】:

    您需要先设置 Http Context。

    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");
    }
    

    【讨论】:

      【解决方案2】:

      从您的命令行运行“播放测试”。这将运行 test 文件夹中的所有测试。

      【讨论】:

      • 这就是我运行测试的方式,我将错误复制到我的帖子中。
      【解决方案3】:

      为什么不使用硒?

      查看帮助的最后部分:http://www.playframework.com/documentation/2.0/JavaFunctionalTest

      【讨论】:

      • 这就是我现在正在做的,我已经放弃了JUnit/Spec。
      猜你喜欢
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多