How to use isA-Matcher

I have a specific method that provides an Restriction-object (where Restrictionis the interface). And since its implementation is already testet, I just want to check if my method really provides an object RestrictionImpl.
I saw that there are helpers that I can use with assertThat, and I thought that isA-matcher is what I need for this task.

My simplified code is as follows:

public static Restriction getRestriction() {
    return new RestrictionImpl();
}

and my test looks like this:

@Test
public void getRestriction_returnsRestrictionImpl() {
    assertThat(getRestriction(), isA(RestrictionImpl.class));
}

However, this does not compile. All I could do was check if it RestrictionImplis Restriction... but that makes no sense.

Do I really not understand the purpose isA? And what does it really mean?

UPDATE:
assertThat(getRestriction(), is(instanceOf(RestrictionImpl.class))) , , isA . assertThat , , , assertThat(T, Matcher<? extends T>), assertThat(T, Matcher<? super T>)

+4
2

, :
https://github.com/hamcrest/JavaHamcrest/issues/27

, isA junit. is(isIntanceOf(...)), .

+4

, instanceOf. , . isA... , . , : hamcrest ? : , .

, :

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

, ? ?

+2

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


All Articles