Issue
all, I have a maven java project with junits: for example:
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import com.jayway.jsonassert.impl.matcher.IsCollectionWithSize;
@Test
public void test() throws Exception {
getMockMvc().perform(put("/bla")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE)
.characterEncoding("UTF-8")
.content("{\n" +
"\n" +
"\"eventTypes\": [\"BLA\"]\n" +
"\n" +
"}"))
.andExpect(jsonPath("$.eventTypes", IsCollectionWithSize.hasSize(1)));
}
the line that run the check: jsonPath("$.eventTypes", IsCollectionWithSize.hasSize(1)
the jsonPath is part of Spring libary verion 4.3.30 and the IsCollectionWithSize.hasSize is taken form com.jayway.jsonpath.json-path-assert verion 2.4
everything worked ok until I upgraded the Spring to 5.3.20 The error is:
incompatible types: cannot infer type-variable(s) T,capture#79 of ? super java.util.Collection<? extends E>,E
[ERROR] (argument mismatch; org.hamcrest.Matcher<capture#80 of ? super java.util.Collection<? extends E>> cannot be converted to org.hamcrest.Matcher<? super java.lang.Object>)
[ERROR] -> [Help 1]
Has anyone had such a problem? And can offer a solution?
Solution
ok so I fixed it by changing one of the imports. instead of this:
import com.jayway.jsonassert.impl.matcher.IsCollectionWithSize;
I used this:
import static org.hamcrest.Matchers.hasSize;
and change the code from this:
andExpect(jsonPath("$.eventTypes", IsCollectionWithSize.hasSize(1)));
to this:
andExpect(jsonPath("$.eventTypes", hasSize(1)))
Answered By - Tal Levi
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)