-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Affects: 2.7.10
Hello everyone.
I'm uncertain if this is an issue with spring-test or with Java compiler itself but, from Spring Boot 2.7.10 (which incorporates spring-test 5.3.26), we have been having an odd issue with tests calling the wrong MockRestRequestMatchers.header() method.
In spring-test 5.3.25 there were a couple of header() methods, but the one that interest us is the one with this signature:
public static RequestMatcher header(String name, Matcher<? super String>... matchers)
this was used in testing like this:
.andExpect(header("X-MY-HEADER", equalTo("myValue")))
where equalTo() is defined in Hamcrest as
public static <T> Matcher<T> equalTo(T operand)
this worked normally.
From Spring Boot 2.7.10 (spring-test 5.3.26) a new header() method has been added with this signature:
public static RequestMatcher header(String name, Matcher<? super List<String>> matcher)
and this is where things are breaking: the same call above is now entering this new method, as opposed to the previous. I find this surprising because the signature doesn't match, equalTo("myValue") returns Matcher<String>, which doesn't align to Matcher<? super List<String>>, and yet it's coming in. If I change the call to header() to explicitly call out the type like this:
.andExpect(header("X-MY-HEADER", CoreMatchers.<String>equalTo("myValue")))
then things go back to normal and the call is again directed to the old header() method.
I'm guessing that, via type erasure, the signatures of the two header() methods are being simplified to just header(String, Matcher) and, there being two possible candidates that can match the call, Java is just picking the first one. I'm not sure what could be done from spring-framework's perspective, though.
Thoughts?