Mr Man
Mr Man

Reputation: 1588

AssertionError when JUnit testing a JSON response

I am creating my first unit test, and just wanted to create something pretty simple based on one of the Spring tutorials.

Here is the error I am getting:

java.lang.AssertionError: Response content
Expected: (null or an empty string)
 but: was "Debug"

My Controller:

@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();
  }

My Test:

@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()));
  }

}

Upvotes: 1

Views: 1261

Answers (1)

devops
devops

Reputation: 9179

in your test your are expecting EmptyOrNullString, but your controller produces a lastName

change your expectation:

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

Upvotes: 2

Related Questions