Java MockHttpServletResponse正文为空
我尝试在我的控制器中测试我的方法Java MockHttpServletResponse正文为空,java,junit,mocking,mockito,junit5,Java,Junit,Mocking,Mockito,Junit5,我尝试在我的控制器中测试我的方法addPerson(),但是当我执行测试时,我的状态为200,在MockHttpServletResponse中有一个空的主体。我想用MockMvcResultMatchers中的jsonPath来测试body响应,但是当body为空时,我不能这样做 这是我的测试: @WebMvcTest(PersonController.class) @ExtendWith(SpringExtension.class) public class PersonController
addPerson()
,但是当我执行测试时,我的状态为200,在MockHttpServletResponse
中有一个空的主体。我想用MockMvcResultMatchers
中的jsonPath
来测试body响应,但是当body为空时,我不能这样做
这是我的测试:
@WebMvcTest(PersonController.class)
@ExtendWith(SpringExtension.class)
public class PersonControllerTest {
@Autowired
private MockMvc mvc;
@Autowired
private Model model;
@MockBean
private PersonService service;
@Test
public void addPersonTest() throws Exception {
this.mvc.perform(post("/person/add")
.contentType(MediaType.APPLICATION_JSON).content("{\"firstName\": \"Test\",\"lastName\": \"\",\"address\": \"\",\"city\": \"\",\"zip\": \"\",\"phone\": \"\",\"email\": \"\"}"))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk());
}
这是我的控制器,使用addPerson()方法
@RequestMapping(“/person”)
@RestController
公共类个人控制器{
@自动连线
模型;
私有静态最终记录器Logger=LogManager.getRootLogger();
@自动连线
私人私人服务;
@GetMapping(“/”)
公共列表allPerson(){
return personService.all();
}
@后映射(“/add”)
公共列表addPerson(@RequestBody-Person){
List listPerson=this.personService.add(person);
info(“Request=@RequestBody={}”,person);
logger.info(“响应{}”,listPerson);
返回列表人;
}
以下是服务:
@Service
public class PersonService {
@Autowired
private Model model;
public PersonService(Model model2) {
this.model = model2;
}
public List<Person> add(Person person) {
List<Person> listPersons = model.getPersons();
listPersons.add(person);
return listPersons;
}
@服务
公共类人员服务{
@自动连线
私有模型;
公共人员服务(模型2){
this.model=model2;
}
公共列表添加(个人){
List listPersons=model.getPersons();
列表人员。添加(人员);
返回名单人员;
}
感谢您的帮助。当您模拟
PersonService时,您必须提供其行为,否则它总是返回null
。您可以使用Mockito提供的when()。然后返回()
@WebMvcTest(PersonController.class)
@ExtendWith(SpringExtension.class)
public class PersonControllerTest {
@Autowired
private MockMvc mvc;
@Autowired
private Model model;
@MockBean
private PersonService service;
@Test
public void addPersonTest() throws Exception {
List<Person> personList = Arrays.asList(new Person()); // create list here
when(service.add(any(Person.class)).thenReturn(personList); // mock the behaviour of your PersonService bean
this.mvc.perform(post("/person/add")
.contentType(MediaType.APPLICATION_JSON).content("{\"firstName\": \"Test\",\"lastName\": \"\",\"address\": \"\",\"city\": \"\",\"zip\": \"\",\"phone\": \"\",\"email\": \"\"}"))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk());
}
}
@WebMvcTest(PersonController.class)
@ExtendWith(SpringExtension.class)
公共类PersonControllerTest{
@自动连线
私有MockMvc;
@自动连线
私有模型;
@蚕豆
私人人事服务;
@试验
public void addPersonTest()引发异常{
List personList=Arrays.asList(new Person());//在此处创建列表
when(service.add(any(Person.class)).thenReturn(personList);//模拟PersonService bean的行为
这个.mvc.perform(post(“/person/add”)
.contentType(MediaType.APPLICATION\u JSON).content(“{”firstName\“:\“Test\”、\“lastName\”:\“\”、\“address\”:\“\”、\“city\”:\”、\“zip\”:“\”、\“phone\”:“\”、\“email\”:“\”))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk());
}
}
当您使用@MockBean注释模拟PersonService时,会导致服务不调用实际的方法,并返回一个空列表。此外,您的add方法不会像您预期的那样工作,因为它不会将列表保存回您的模型。
@WebMvcTest(PersonController.class)
@ExtendWith(SpringExtension.class)
public class PersonControllerTest {
@Autowired
private MockMvc mvc;
@Autowired
private Model model;
@MockBean
private PersonService service;
@Test
public void addPersonTest() throws Exception {
List<Person> personList = Arrays.asList(new Person()); // create list here
when(service.add(any(Person.class)).thenReturn(personList); // mock the behaviour of your PersonService bean
this.mvc.perform(post("/person/add")
.contentType(MediaType.APPLICATION_JSON).content("{\"firstName\": \"Test\",\"lastName\": \"\",\"address\": \"\",\"city\": \"\",\"zip\": \"\",\"phone\": \"\",\"email\": \"\"}"))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk());
}
}