Java JUnit测试JSON响应时的AssertionError

Java JUnit测试JSON响应时的AssertionError,java,json,spring,junit,Java,Json,Spring,Junit,我正在创建我的第一个单元测试,只是想在Spring教程的基础上创建一些非常简单的东西 以下是我得到的错误: java.lang.AssertionError: Response content Expected: (null or an empty string) but: was "Debug" 我的控制器: @RestController @RequestMapping(value = "/person") public class PersonController { @Auto

我正在创建我的第一个单元测试,只是想在Spring教程的基础上创建一些非常简单的东西

以下是我得到的错误:

java.lang.AssertionError: Response content
Expected: (null or an empty string)
 but: was "Debug"
我的控制器:

@RestController
@RequestMapping(value = "/person")
public class PersonController {

  @Autowired
  protected PersonService personService;

  @RequestMapping(value = "/lastName", produces = "application/json")
  public String lastName(@RequestParam(value = "cid") String cid)
  {
    return personService.findByCId(cid).getLastName();
  }
我的测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MvcLastNameTest {

@Autowired
  private MockMvc mockMvc;

  @Test
  public void shouldReturnNonNullString() throws Exception {
    this.mockMvc.perform(get("/person/lastName?cid=123456")).andDo(print()).andExpect(status().isOk())
        .andExpect(content().string(isEmptyOrNullString()));
  }

}

在您的测试中,您需要
EmptyOrNullString
,但您的控制器会生成一个
lastName

改变你的期望:

.andExpect(content().string("Debug")); // or any other valid lastName

那么您希望得到什么结果呢?在我看来,您可能不需要在测试之前模拟对personService.findByCId()的调用,除非您必须为测试在内存中设置DB,这意味着对于cid=123456的用户,其姓氏是“Debug”,我真的很笨。。。请原谅我提出了这个可怕的问题。阅读了ya'lls的评论后,我记得我们的测试用户的名字是“Tom Debug”,所以他的姓是Debug,它不会返回Debug,因为我认为存在一些虚构的JSON调试/