Assert an object is a specific type

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 Object1 or a type of Object2?

Currently I have:

assertTrue(myObject instanceof Object1);
assertTrue(myObject instanceof Object2);

This works but I was wondering if there is a more expressive way of doing this.

For example something like:

assertObjectIsClass(myObject, Object1);

I could do this:

assertEquals(myObject.class, Object1.getClass());

Is there a specific assert method that allows me to test a type of an object in a more elegant, fluid manner?

6 Answers
6

Leave a Comment