The reason your original code doesn't compile is that <? extends Serializable>
does not mean, "any class that extends Serializable," but "some unknown but specific class that extends Serializable."
For example, given the code as written, it is perfectly valid to assign new TreeMap<String, Long.class>()
to expected
. If the compiler allowed the code to compile, the assertThat()
would presumably break because it would expect Date
objects instead of the Long
objects it finds in the map.