-1

I have a following test method in mockMVC

I need to improve the tests to make sense. at the moment tests are not testing, how to improve to test? just add verification of data compliance? how to add it? What could be the example of data validation verification? how to test if UUID agrees?

how to improve these tests?

martine
  • 79
  • 2
  • 3
  • 10

1 Answers1

0

You could do something like this:

@Test
public void createUser() throws Exception {
    User user = userRepository.save(new User("testname", "testemail", 10));
    String userJson = toJson(user);

    this.mockMvc.perform(post("/user/add/")
            .contentType(contentType)
            .content(userJson))
            .andExpect(status().isCreated());
}

or

@Test
public void getUserById() throws Exception {
    User user = userRepository.save(new User("testname", "testemail", 10));   
    mockMvc.perform(get("/user/" + user.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(contentType))
            .andExpect(jsonPath("$.id", is(user.getId())))
            .andExpect(jsonPath("$.username", is(user.getUsername())))
            .andExpect(jsonPath("$.age", is(user.getAge())))
            .andExpect(jsonPath("$.emailAddress", is(user.getEmailAddress())));
}

You can check whole test class in my github repo

Hopefully this will help you

vanillaSugar
  • 523
  • 1
  • 5
  • 14
  • I have a bug java.lang.AssertionError: No value at JSON path "$.version" at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:290) at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:74) at org.springframework.test.web.servlet.result.JsonPathResultMatchers.lambda$value$0(JsonPathResultMatchers.java:87) at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:179) – martine May 19 '18 at 19:11
  • i added .andExpect(jsonPath("$.version", is("0.1"))) .andExpect(jsonPath("$.email", is(accountCreatedDTO .getEmail()))) .andExpect(jsonPath("$.name", is(accountCreatedDTO .getName()))) – martine May 19 '18 at 19:12
  • Try to print returned json and see what's going on – vanillaSugar May 19 '18 at 19:15
  • at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) – martine May 19 '18 at 19:22
  • Caused by: net.minidev.json.parser.ParseException: Unexpected End Of File position 63963: null at net.minidev.json.parser.JSONParserMemory.readString(JSONParserMemory.java:122) at net.minidev.json.parser.JSONParserBase.readObject(JSONParserBase.java:526) at net.minidev.json.parser.JSONParserBase.readMain(JSONParserBase.java:403) at net.minidev.json.parser.JSONParserBase.readObject(JSONParserBase.java:546) at net.minidev.json.parser.JSONParserBase.readMain(JSONParserBase.java:403) at net.minidev.json.parser.JSONParserBase.readObject(JSONParserBase.java:546) – martine May 19 '18 at 19:23
  • I can't help you like this it is impossible. This is one of the moments when you have to help yourself, that's how you learn – vanillaSugar May 19 '18 at 19:26
  • *** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844 – martine May 19 '18 at 19:31