Java 运行JUnit/Mockito测试时出现org.springframework.http.converter.httpMessageNodeReadableException
我在Spring引导应用程序中使用Mockito运行JUnit测试。我在模拟控制器应该调用的存储库。当运行响应代码为400的POST测试时,我得到了一个HttpMessageOnTreadableException。GET可以正常工作Java 运行JUnit/Mockito测试时出现org.springframework.http.converter.httpMessageNodeReadableException,java,spring,spring-boot,junit,mockito,Java,Spring,Spring Boot,Junit,Mockito,我在Spring引导应用程序中使用Mockito运行JUnit测试。我在模拟控制器应该调用的存储库。当运行响应代码为400的POST测试时,我得到了一个HttpMessageOnTreadableException。GET可以正常工作 package coffee; import static org.junit.Assert.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.j
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);
}
}
看起来关于json的命令无效。您可以在content方法中使用带双引号的json吗
{ "id":null, "position":null, "name":"Test", "description":null, "image":null }
看起来关于json的命令无效。您可以在content方法中使用带双引号的json吗?{id:null,position:null,name:Test,description:null,image:null}@Jayesh工作过,如果你作为答案回复,我会接受。非常感谢。
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);
}
}
{ "id":null, "position":null, "name":"Test", "description":null, "image":null }