0

I am getting the following error trace, when I run a JUNIT method for HTTP PUT request in Spring boot.

The input JSON value is successfully passed to the service method. However, I am not getting any response in HTTP servlet response.

When I hit the service URL in post man, the input to the request will be JSON and response we get is also JSON.

PUT request

    @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

Error trace

        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)

Below is the console log. As we can see here, input json is passed but in response , Body is empty

Console log

   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.
Vidhya
  • 1
  • 2
  • Is your controller a `@Controller` or a `@RestController`? If it is a @Controller, maybe your problem is the same as in here: https://stackoverflow.com/questions/22183178/java-lang-assertionerror-content-type-not-set-while-junit-spring-mvc-controller – Ariel Kohan Aug 21 '18 at 14:38
  • So maybe you are missing the `@ResponseBody` annotation – Ariel Kohan Aug 21 '18 at 14:38
  • @ArielKohan I have used `@RestController` in my controller class. Even after using `@ResponseBody` in the service class, same issue persists. – Vidhya Aug 22 '18 at 06:20
  • Ok and what if you: - Option 1: add an `.accept("application/json;charset=UTF-8")` to your `mockmcv.perform(..` - Option 2: add a `produces = MediaType.APPLICATION_JSON_UTF8_VALUE` to your `@RequestMapping` in the method in the rest controller. – Ariel Kohan Aug 22 '18 at 10:32
  • It looks like your response body is empty. I've observed that if a `@ResponseBody` method produces a null value Spring will generate an empty body and not set the content type. Ensuring some non-null value resolved this problem. – mkjeldsen Jun 10 '19 at 18:27

0 Answers0