How to use the isA-Matcher
Asked Answered
A

2

10

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>)

Anneal answered 16/8, 2016 at 18:29 Comment(3)
I think that what you mean is assertThat(getRestriction().getClass(), isA(RestrictionImpl.class)) or assertThat(getRestriction(), isA(instanceOf(RestrictionImpl.class))).Rondi
both wont compile either. Hower testing assertThat(getRestriction(), is(instanceOf(RestrictionImpl.class)) would work but I though isA is a shortcut to that.Anneal
The signature of assertThat is assertThat(T actual, Matcher<? super T> matcher) requires the type of Matcher should be <? super T>. But in your code, RescrictionImpl.class is not a type Rescriction.class, so it compiles failed. To solve this problem, You can either change your code to assertThat(getRestriction(), isA(Restriction.class)); or change the signature of the getRestriction to public static RestrictionImpl getRestriction().Stormproof
A
12

I found an issue describing my problem:
https://github.com/hamcrest/JavaHamcrest/issues/27

And it looks like isA simply has the wrong signature in this version of junit. It is meant to be shortcut for is(isIntanceOf(...)), but it is not.

Anneal answered 17/8, 2016 at 4:37 Comment(0)
F
2

Probably you want use instanceOf. And you know, those things all have javadoc available in the open. Where isA ... should be exactly what you need. So the problem might be: do you have the required hamcrest core matchers library in your project setup? In other words: maybe you should read this here.

And just some example code, from one of my own projects:

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
...
@Test
public void testWhatever() throws IOException, ApiException {
    try { ...
        fail("should have thrown");
    } catch (IllegalStateException e) {
        e.printStackTrace(); // as expected
        assertThat(e.getCause(), is(instanceOf(SomeClass.class)));

So, do you have those imports there? Do you have the libraries in your project setup to back those imports?

Fold answered 16/8, 2016 at 18:31 Comment(4)
"isA(java.lang.Class<T> type) A shortcut to the frequently used is(instanceOf(SomeClass.class))."Anneal
I was also wondering about the signature of assertThat since it is assertThat(T, Matcher<? super T> but I want to use it like assertThat(T, Matcher<? extends T>Anneal
And yes, I have all dependencies and I have all the imports. With is(instanceOf(RestrictionImpl.class)) it would work... but I thought isA is a shortcut for exactly thatAnneal
Then the only thing left would be questioning the exact versions of the libraries.Fold

© 2022 - 2024 — McMap. All rights reserved.