【发布时间】:2026-01-26 11:50:01
【问题描述】:
我的测试返回失败并出现错误:
java.lang.AssertionError: Content type not set
我读到这个错误并不意味着太多,因为如果存在 NPE 或与处理程序不匹配,则返回相同的... 但我不明白错误在哪里,在我看来一切正常......
我的控制器已经使用@RestController
这是控制器方法:
@RestController
@RequestMapping("/local")
public class ControllerTest{
.....
@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public JsonResponse<TestResponseModel> test (
@RequestParam(value = "data1", required = true) String data1,
@RequestParam(value = "data2", required = true) String data2,
{
try {
TestRequestModel input= new TestRequestModel();
input.setData1(data1);
input.setData2(data2);
TestResponseModel entities = business.test(input);
return JsonResponse(entities);
} catch (TestException e) {
log.error("APPLICATION EXCEPTION", e);
throw e;
} catch (Exception e) {
log.error("UNHANDLED EXCEPTION", e);
throw new TestException(e);
}
}
}
这是测试:
@Mock
private Business business;
@Mock
private TestResponseModel testResponseModel;
@Test
public void getTest() throws Exception {
when(business.test(isA(TestRequestModel.class))).thenReturn(testResponseModel);
mockMvc.perform(get("/local/test")
.requestAttr("data1", "data1_test")
.requestAttr("data2", "data2_test"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"));
}
堆栈跟踪:
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:35)
at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:65)
at org.springframework.test.web.servlet.result.ContentResultMatchers$1.match(ContentResultMatchers.java:82)
at org.springframework.test.web.servlet.MockMvc.applyDefaultResultActions(MockMvc.java:191)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:163)
at com.test.controller.TestClassTest.GetTest(TestClassTest.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:68)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:74)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)
【问题讨论】:
-
你能分享更多的堆栈跟踪吗?
标签: java mockito spring-test