Spring Boot模拟MVC断言错误:内容类型未设置

时间:2018-08-21 12:56:35

标签: java spring spring-boot junit mockmvc

当我在Spring Boot中为HTTP PUT请求运行JUNIT方法时,出现以下错误跟踪。

输入的JSON值已成功传递到服务方法。但是,HTTP servlet响应中没有任何响应。

当我点击邮递员中的服务URL时,请求的输入将为JSON,而我们得到的响应也是JSON。

PUT请求

    @Test
     public void updateMilestoneTest() throws Exception {
         Milestone milestone = new Milestone("100","MSJunit2","description","test2","mahi","mahi", null);
         milestone.setCreatedBy("auth");
         milestone.setCreatedDate(null);
         milestone.setUpdatedBy("auth");
         milestone.setLastModifiedDate(null);     
         when(milestoneService.updateMilestone("100",milestone)).thenReturn(milestone);
         String updateMilestoneJSON = "{\"milestoneId\":100,\"milestoneName\":\"MSJunit2\",\"description\":\"description\",\"condition\":\"test2\",\"createdBy\":\"auth\",\"updatedBy\":\"auth\",\"milestoneSynonymMappings\":null}";
         mockMvc.perform(put("/milestone/updateMilestone/100").contentType("application/json;charset=UTF-8").content(updateMilestoneJSON)).andExpect(status().isOk())
         .andExpect(content().contentType("application/json;charset=UTF-8"))
         .andExpect(jsonPath("$.milestoneId").value(100)).andExpect(jsonPath("$.milestoneName").value("MSJunit2"))
         .andExpect(jsonPath("$.description").value("description")).andExpect(jsonPath("$.createdBy").value("auth"));
     }

Below is the error trace. I hope we should set content type as application/json only

错误跟踪

        java.lang.AssertionError: Content type not set
            at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:36)
            at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:66)
            at org.springframework.test.web.servlet.result.ContentResultMatchers.lambda$contentType$0(ContentResultMatchers.java:79)
            at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:178)
            at com.controller.MilestoneControllerTest.updateMilestoneTest(MilestoneControllerTest.java:102)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
            at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
            at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
            at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
            at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
            at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
            at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
            at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
            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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
            at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
            at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
            at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
            at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
            at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

下面是控制台日志。正如我们在这里看到的那样,传递了json输入,但作为响应,Body为空

控制台日志

   MockHttpServletRequest:
      HTTP Method = PUT
      Request URI = /milestone/updateMilestone/100
       Parameters = {}
          Headers = {Content-Type=[application/json;charset=UTF-8]}
             Body = {"milestoneId":100,"milestoneName":"MSJunit2","description":"description","condition":"test2","createdBy":"auth","updatedBy":"auth","milestoneSynonymMappings":null}
    Session Attrs = {}

Handler:
             Type = com.controller.MilestoneController
           Method = public com.domain.Milestone com.controller.MilestoneController.updateMilestone(com.domain.Milestone,java.lang.String) throws com.exception.EntityNotFoundException

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

Please let me know for further details needed. Thanks in advance.

0 个答案:

没有答案