Joobie:如何根据 MediaType 正确地对 returns 不同内容的路由进行单元测试?
Joobie: How to properly unit test a route that returns different content depending on MediaType?
我目前正在尝试使用 Jooby 编写 webapps/apis。我将其设置为 returns 帐户数据的端点,根据接受的 header 值作为 HTML 或 JSON。此端点有效并且 return 提供了正确的信息。
在 JUnit 中编写单元测试时,如何将 accept header 值与我的 get 请求一起传递,以便我可以正确地测试请求中 return 的内容?
我已经尝试使用 Mockito 来模拟一个请求 object 和 return 对请求的各种调用的响应 object 但我似乎无法在文档中找到 Jooby 如何当您使用 Results.when 方法时,在它自己的请求 object 中测试 header 值。
这是我的终点:
get("/allAccounts", () ->
Results
.when(MediaType.html, () -> Results.html("display").put("accounts", accounts))
.when(MediaType.json, () -> accounts)
.when("*", () -> Status.NOT_ACCEPTABLE)
);
到目前为止尝试的测试都与以下类似。我已经尝试了许多不同的方法来代替 'type()',例如 .accept() 但是 none 似乎被查询,因为 MockRouter 的 get() 方法在任何这些条件下都不会 returns 一个字符串
@Test
public void allAccountsHTMLunitTest() throws Throwable {
Request req = mock(Request.class);
when(req.type()).thenReturn(MediaType.html);
String result = new MockRouter(new App(), req)
.get("/allAccounts");
assertEquals(// some assertion );
}
我期望(也许是错误的)当我通过 MockRouter 和 header 发出 get 请求时 "accept: text/html" 或 "accept: application/json" 它应该 return分别包含 html 或 json 的字符串。
相反,我在尝试将结果 object 转换为字符串时遇到错误。
我是不是真的误会了?
没错,这基本上就是单元测试和集成测试之间的区别。
对于单元测试,所有 MockRouter
所做的就是调用路由处理函数,函数 /allAccounts
returns 一个结果对象,这就是为什么你得到一个 class 转换异常。
这是您的示例,但使用 Result
通过 result.get()
访问值
@Test
public void allAccountsHTMLunitTest() throws Throwable {
Request req = mock(Request.class);
when(req.type()).thenReturn(MediaType.html);
Result result = new MockRouter(new App(), req)
.get("/allAccounts");
View view = result.get();
assertEquals("display", view.name());
assertEquals("{accounts=[1, 2]}", view.model().toString());
}
希望这对您有所帮助。
我目前正在尝试使用 Jooby 编写 webapps/apis。我将其设置为 returns 帐户数据的端点,根据接受的 header 值作为 HTML 或 JSON。此端点有效并且 return 提供了正确的信息。
在 JUnit 中编写单元测试时,如何将 accept header 值与我的 get 请求一起传递,以便我可以正确地测试请求中 return 的内容?
我已经尝试使用 Mockito 来模拟一个请求 object 和 return 对请求的各种调用的响应 object 但我似乎无法在文档中找到 Jooby 如何当您使用 Results.when 方法时,在它自己的请求 object 中测试 header 值。
这是我的终点:
get("/allAccounts", () ->
Results
.when(MediaType.html, () -> Results.html("display").put("accounts", accounts))
.when(MediaType.json, () -> accounts)
.when("*", () -> Status.NOT_ACCEPTABLE)
);
到目前为止尝试的测试都与以下类似。我已经尝试了许多不同的方法来代替 'type()',例如 .accept() 但是 none 似乎被查询,因为 MockRouter 的 get() 方法在任何这些条件下都不会 returns 一个字符串
@Test
public void allAccountsHTMLunitTest() throws Throwable {
Request req = mock(Request.class);
when(req.type()).thenReturn(MediaType.html);
String result = new MockRouter(new App(), req)
.get("/allAccounts");
assertEquals(// some assertion );
}
我期望(也许是错误的)当我通过 MockRouter 和 header 发出 get 请求时 "accept: text/html" 或 "accept: application/json" 它应该 return分别包含 html 或 json 的字符串。
相反,我在尝试将结果 object 转换为字符串时遇到错误。
我是不是真的误会了?
没错,这基本上就是单元测试和集成测试之间的区别。
对于单元测试,所有 MockRouter
所做的就是调用路由处理函数,函数 /allAccounts
returns 一个结果对象,这就是为什么你得到一个 class 转换异常。
这是您的示例,但使用 Result
通过 result.get()
@Test
public void allAccountsHTMLunitTest() throws Throwable {
Request req = mock(Request.class);
when(req.type()).thenReturn(MediaType.html);
Result result = new MockRouter(new App(), req)
.get("/allAccounts");
View view = result.get();
assertEquals("display", view.name());
assertEquals("{accounts=[1, 2]}", view.model().toString());
}
希望这对您有所帮助。