模拟静态方法时出现内容类型错误
Content type error when mocking static method
我正在尝试测试控制器内的方法。
如果我在我正在测试的方法中调用的静态方法中注释掉逻辑,则测试通过。
我不能评论那个逻辑,相反我只想嘲笑它。现在模拟工作了,
但我收到如下新错误:
java.lang.AssertionError: Content type not set
但我确实有指示的内容类型。请告知我做错了什么。
@Test
public void testMethod() throws Exception{
// If I don't mock this, test will fail.
// If I don't mock this comment out logic in this method, test passes.
// If I mock this, test passes if I don't check for content type.
// I am using Power Mockito.
mockStatic(MyStaticClass.class);
doReturn("").when(MyStaticClass.class, "someMethod", any(Config.class), anyString());
//also tried this, works.
//when(MyStaticClass.someMethod(any(Config.class), anyString())).thenReturn("");
//as mentioned above this would work if I comment out logic in MyStaticClass.
mockMvc.perform(
get("/api/some/a/b/c/d").accept(
MediaType.APPLICATION_JSON))
.andExpect(status().isForbidden())
.andExpect(content().contentType("text/html")); // when I mock, I need to comment this out to get test to work.
}
// Controller
@RequestMapping(value = "/{a}/{b}/{c}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) // I do have content type
@ResponseBody
public MyResponse getSomething(
HttpServletRequest request, HttpServletResponse response,
@PathVariable String a, @PathVariable String b,
@PathVariable String c,
@RequestParam(value = "some", required = false) String some)
throws Exception {
// some logic
//static method being called
MyStaticClass.someMethod("sample", "sample2");
try {
MyResponse myPageResponse = new MyResponse(anotherStr, someStr); // it breaks here and throws that error msg. Doesn't reach return.
return MyResponse;
} catch (NullPointerException npe) {}
}
这是一个没有 body 的获取请求,因此理想情况下使用 contentType("text/html") 指定 content-type header 可能不是正确的方法。
第二
您在请求中的 content-type header 必须与 @consumes 值匹配,它清楚地表明您希望发送 text/html 但没有 @consumes 支持它。
我正在尝试测试控制器内的方法。 如果我在我正在测试的方法中调用的静态方法中注释掉逻辑,则测试通过。
我不能评论那个逻辑,相反我只想嘲笑它。现在模拟工作了, 但我收到如下新错误:
java.lang.AssertionError: Content type not set
但我确实有指示的内容类型。请告知我做错了什么。
@Test
public void testMethod() throws Exception{
// If I don't mock this, test will fail.
// If I don't mock this comment out logic in this method, test passes.
// If I mock this, test passes if I don't check for content type.
// I am using Power Mockito.
mockStatic(MyStaticClass.class);
doReturn("").when(MyStaticClass.class, "someMethod", any(Config.class), anyString());
//also tried this, works.
//when(MyStaticClass.someMethod(any(Config.class), anyString())).thenReturn("");
//as mentioned above this would work if I comment out logic in MyStaticClass.
mockMvc.perform(
get("/api/some/a/b/c/d").accept(
MediaType.APPLICATION_JSON))
.andExpect(status().isForbidden())
.andExpect(content().contentType("text/html")); // when I mock, I need to comment this out to get test to work.
}
// Controller
@RequestMapping(value = "/{a}/{b}/{c}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) // I do have content type
@ResponseBody
public MyResponse getSomething(
HttpServletRequest request, HttpServletResponse response,
@PathVariable String a, @PathVariable String b,
@PathVariable String c,
@RequestParam(value = "some", required = false) String some)
throws Exception {
// some logic
//static method being called
MyStaticClass.someMethod("sample", "sample2");
try {
MyResponse myPageResponse = new MyResponse(anotherStr, someStr); // it breaks here and throws that error msg. Doesn't reach return.
return MyResponse;
} catch (NullPointerException npe) {}
}
这是一个没有 body 的获取请求,因此理想情况下使用 contentType("text/html") 指定 content-type header 可能不是正确的方法。 第二 您在请求中的 content-type header 必须与 @consumes 值匹配,它清楚地表明您希望发送 text/html 但没有 @consumes 支持它。