Using common limited substitutions when testing cards with junit

I am trying to understand why this junit statement gives me a compile-time error:

Map<String, Set<String>> actual = methodToTest();
assertThat(result, hasEntry("foo", new HashSet<String>(Arrays.asList("bar"))));

If I write it like this, it works fine:

Map<String, Set<String>> actual = methodToTest();
Set<String> expected = new HashSet<String>(Arrays.asList("bar"));
assertThat(result, hasEntry("foo", expected));

The compiler error from the first example:

The method assertThat(T, Matcher<? super T>) in the type Assert is not
applicable for the arguments (Map<String,Set<String>>, Matcher<Map<?
extends String,? extends HashSet<String>>>)

HashSet<String>is a subtype Set<String>, so why doesn't it work?

+4
source share
1 answer

HashSet<String>is a subtype of Set<String>true.

However, Matcher<Map<String,HashSet<String>>> not a subset Matcher<Map<String,Set<String>>>. Remember that a is List<String>not a subtype List<Object>.

The method assertThatexpects an argument of type Matcher<? super Map<String, Set<String>>>that is incompatible with Matcher<Map<String,HashSet<String>>>.

+4
source

Source: https://habr.com/ru/post/1609043/


All Articles