I have a certain method that delivers a Restriction
-object (where Restriction
is an interface). And since its implementation is already testet, I just want to test if my method actually delivers a RestrictionImpl
-object.
I saw that there are matchers which I can use together with assertThat
and I thought, that the isA
-matcher is the thing want for this task.
Simplified my code looks like that:
public static Restriction getRestriction() {
return new RestrictionImpl();
}
and my test looks like that;
@Test
public void getRestriction_returnsRestrictionImpl() {
assertThat(getRestriction(), isA(RestrictionImpl.class));
}
However this won't compile. All I could do is test, if a RestrictionImpl
is a Restriction
... but there is no point in doing this.
Am I misunderstanding to purpose of isA
? And what is it acually meant for?
UPDATE:
Using assertThat(getRestriction(), is(instanceOf(RestrictionImpl.class)))
would work, but I thought that isA
is a shortcut for exactly that.
Calling assertThat
in the way I want would require it to have the signature assertThat(T, Matcher<? extends T>)
, but its signature is assertThat(T, Matcher<? super T>)
assertThat(getRestriction().getClass(), isA(RestrictionImpl.class))
orassertThat(getRestriction(), isA(instanceOf(RestrictionImpl.class)))
. – RondiassertThat(getRestriction(), is(instanceOf(RestrictionImpl.class))
would work but I thoughisA
is a shortcut to that. – AnnealassertThat(T actual, Matcher<? super T> matcher)
requires the type of Matcher should be <? super T>. But in your code,RescrictionImpl.class
is not a typeRescriction.class
, so it compiles failed. To solve this problem, You can either change your code toassertThat(getRestriction(), isA(Restriction.class));
or change the signature of the getRestriction topublic static RestrictionImpl getRestriction()
. – Stormproof