org.springframework.http.converter.httpmessagenotradableExce



im在我的春季启动应用程序中使用Mockito进行JUNIT测试。我正在嘲笑控制器应该调用的存储库。在运行400个响应代码(Get Works Fiest(运行后测试时,我将获得httpmessagenotreadable Exception。

package coffee;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.*;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import coffee.controller.AboutController;
import coffee.data.AboutRepository;
@RunWith(SpringRunner.class)
@WebMvcTest(value = AboutController.class, secure = false)
public class AboutControllerTest {
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private AboutRepository aboutRepository;
    List<About> res;
    @Before
    public void setUp() {
        res = new ArrayList<About>();
        About about = new About();
        about.setName("Test");
        res.add(about);
    }
    @Test
    public void postAbouts() throws Exception{
        About about = res.get(0);
        Mockito.when(aboutRepository.save(about))
            .thenReturn(about);
        RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/abouts")
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .content("{'id':null,'position':null,'name':'Test','description':null,'image':null}");
        MvcResult result = mockMvc.perform(requestBuilder)
            .andExpect(status().isOk())
            .andReturn();
        JSONAssert.assertEquals("{'id':null,'position':null,'name':'Test','description':null,'image':null}", 
                result.getResponse().getContentAsString(),
                false);
    }
}

    MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /abouts
       Parameters = {}
          Headers = {Content-Type=[application/json], Accept=[application/json]}
             Body = <no character encoding set>
    Session Attrs = {}
Handler:
             Type = coffee.controller.AboutController
           Method = public coffee.About coffee.controller.AboutController.postAbout(coffee.About)
Async:
    Async started = false
     Async result = null
Resolved Exception:
             Type = org.springframework.http.converter.HttpMessageNotReadableException
ModelAndView:
        View name = null
             View = null
            Model = null
FlashMap:
       Attributes = null
MockHttpServletResponse:
           Status = 400
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []
2019-05-21 14:47:56.035  INFO 1977 --- [       Thread-4] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@7fd50002: startup date [Tue May 21 14:47:54 PDT 2019]; root of context hierarchy

这是正在测试的控制器

package coffee.controller;
import coffee.*;
import coffee.data.AboutRepository;
import java.util.Optional;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path="abouts",
                produces="application/json",
                consumes="application/json")
@CrossOrigin(origins="*")
public class AboutController {
    private AboutRepository aboutRepo;
    public AboutController(AboutRepository aboutRepo) {
        this.aboutRepo = aboutRepo;
    }
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public About postAbout(@RequestBody About about) {
        return aboutRepo.save(about);
    }
}

看起来大约是JSON无效。您可以在内容方法上使用双引号的JSON尝试吗?

{ "id":null, "position":null, "name":"Test", "description":null, "image":null }

相关内容

  • 没有找到相关文章

最新更新