9

I am practicing MockMVC for rest call unit testing. how can we test the Boolean values so that whether the result is true or false I need to pass the test, I tried as follows,

mockMvc.perform(get("/student/{Id}", 1L)).
.andExpect(status().isOk())
.andExpect(jsonPath("$.isPass", is(true  || false)));

Also I have list with 6 values, how can use list contains all kind of method,

.andExpect(jsonPath("$.subjectList", hasSize(5)))
.andExpect(jsonPath("$.subjectList.name", Matchers.contains("English", "Hindi", "France", "Tamil", "Bengali"))

Any suggestions please!!

4
  • I assume, that your code just misses a method call: ... .andExpect(jsonPath("$.isPass", is(true || false)).exists()); Commented Nov 23, 2015 at 11:24
  • I tried your suggestion. It is not working for me Commented Nov 30, 2015 at 9:10
  • What error are you getting ? Commented Nov 30, 2015 at 16:54
  • can you provide the student class Commented Dec 1, 2015 at 5:36

1 Answer 1

8
+50

I would suggest the use of hamcrest AnyOf logical matcher

Form the Tutorial :

anyOf - matches if any matchers match, short circuits (like Java ||)

So in your case:

import static org.hamcrest.core.AnyOf.*;

mockMvc.perform(get("/student/{Id}", 1L)).
.andExpect(status().isOk())
.andExpect(jsonPath("$.isPass", anyOf(is(false),is(true))));
.andExpect(jsonPath("$.subjectList.name", anyOf(is("English"),is("Hindi")…)));

Somtime the usage of hamcrest with Junit and some mock libs can be tricky see

How to use JUnit and Hamcrest together?

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.