1

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

}
3
  • 1
    And what result do you expect? Commented Jan 10, 2018 at 15:18
  • 1
    From my point of view, you might lack to mock the comportment of your call to personService.findByCId() prior to your test, unless you have to in-memory DB set up for your test, which would mean that for the user with the cid=123456, its last name is "Debug" Commented Jan 10, 2018 at 15:19
  • I am really dumb... Please forgive me for my awful question. Reading ya'lls comments made me remember that our test user's name is "Tom Debug", so his last name is Debug and it isn't returning Debug because some imaginary JSON debugging I was thinking existed. :/ Commented Jan 10, 2018 at 16:07

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.