gpt4 book ai didi

java - PlayFramework 2 模板测试

转载 作者:行者123 更新时间:2023-11-30 11:20:18 26 4
gpt4 key购买 nike

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

最佳答案

您需要先设置 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");
}

关于java - PlayFramework 2 模板测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22791538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com