Assert an object is a specific type

后端 未结 4 555
滥情空心
滥情空心 2020-12-02 07:35

Is it possible in JUnit to assert an object is an instance of a class? For various reasons I have an object in my test that I want to check the type of. Is it a type of Obje

相关标签:
4条回答
  • 2020-12-02 08:12

    Solution for JUnit 5

    The documentation says:

    However, JUnit Jupiter’s org.junit.jupiter.Assertions class does not provide an assertThat() method like the one found in JUnit 4’s org.junit.Assert class which accepts a Hamcrest Matcher. Instead, developers are encouraged to use the built-in support for matchers provided by third-party assertion libraries.

    Example for Hamcrest:

    import static org.hamcrest.CoreMatchers.instanceOf;
    import static org.hamcrest.MatcherAssert.assertThat;
    
    import org.junit.jupiter.api.Test;
    
    class HamcrestAssertionDemo {
    
        @Test
        void assertWithHamcrestMatcher() {
            SubClass subClass = new SubClass();
            assertThat(subClass, instanceOf(BaseClass.class));
        }
    
    }
    

    Example for AssertJ:

    import static org.assertj.core.api.Assertions.assertThat;
    
    import org.junit.jupiter.api.Test;
    
    class AssertJDemo {
    
        @Test
        void assertWithAssertJ() {
            SubClass subClass = new SubClass();
            assertThat(subClass).isInstanceOf(BaseClass.class);
        }
    
    }
    

    Note that this assumes you want to test behaviors similar to instanceof (which accepts subclasses). If you want exact equal type, I don’t see a better way than asserting the two class to be equal like you mentioned in the question.

    0 讨论(0)
  • 2020-12-02 08:16

    Since assertThat which was the old answer is now deprecated, I am posting the correct solution:

    assertTrue(objectUnderTest instanceof TargetObject);

    0 讨论(0)
  • 2020-12-02 08:18

    Solution for JUnit 5 for Kotlin!

    Example for Hamcrest:

    import org.hamcrest.CoreMatchers
    import org.hamcrest.MatcherAssert
    import org.junit.jupiter.api.Test
    
    class HamcrestAssertionDemo {
    
        @Test
        fun assertWithHamcrestMatcher() {
            val subClass = SubClass()
            MatcherAssert.assertThat(subClass, CoreMatchers.instanceOf<Any>(BaseClass::class.java))
        }
    
    }
    

    Example for AssertJ:

    import org.assertj.core.api.Assertions.assertThat
    import org.junit.jupiter.api.Test
    
    class AssertJDemo {
    
        @Test
        fun assertWithAssertJ() {
            val subClass = SubClass()
            assertThat(subClass).isInstanceOf(BaseClass::class.java)
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 08:21

    You can use the assertThat method and the Matchers that comes with JUnit.

    Take a look at this link that describes a little bit about the JUnit Matchers.

    Example:

    public class BaseClass {
    }
    
    public class SubClass extends BaseClass {
    }
    

    Test:

    import org.junit.Test;
    
    import static org.hamcrest.CoreMatchers.instanceOf;
    import static org.junit.Assert.assertThat;
    
    /**
     * @author maba, 2012-09-13
     */
    public class InstanceOfTest {
    
        @Test
        public void testInstanceOf() {
            SubClass subClass = new SubClass();
            assertThat(subClass, instanceOf(BaseClass.class));
        }
    }
    
    0 讨论(0)
提交回复
热议问题