REST Controller test: MockMvc.andExpect doesn't work with ENUM

Viewed 633

I am using @WebMvcTest and MockMvc mockMvc;

I am trying to do:

  mockMvc.perform(get("/api/v2/developers/12"))
                    .andExpect(jsonPath("$.sportType").value("FOOTBAL"))

But i get:

java.lang.AssertionError: JSON path "$.sportType" 
Expected :FOOTBALL
Actual   :FOOTBALL

FOOTBAL - is enum value, i also tryied to use instead "FOOTBAL":

.andExpect(jsonPath("$.sportType").value(SportType.FOOTBALL))
//OR
.andExpect(jsonPath("$.sportType").value(String.valueOf(SportType.FOOTBALL)))
//OR
.andExpect(jsonPath("$.sportType").value(Arrays.asList(SportType.FOOTBALL)))

How looks my JSON :

{
    "name": "JOHN",
    "sportType": "FOOTBALL"
}
2 Answers

@Deadpool answer is correct. But instead to compare with string i would suggest to use the .name() / .toString() method from ur enum.

anExpect(jsonPath("$.sportType"), is(SportType.FOOTBALL.name()))

Since the response is JSON string and sportType is string type i will recommend to compare it as string instead of enum

andExpect(jsonPath("$.sportType").value("FOOTBALL"))

Or you can also use Matchers

andExpect(jsonPath("$.sportType", is("FOOTBALL")))
Related