Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8535115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:21:28+00:00 2026-06-11T10:21:28+00:00

Is it possible in JUnit to assert an object is an instance of a

  • 0

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(Object1.class, myObject.getClass());

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:21:29+00:00Added an answer on June 11, 2026 at 10:21 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: JUnit test for System.out.println() Is it possible to check, through JUnit testing,
I have a Spring-enabled JUnit Test. It loads my default applicationContext. Now I want
I have a JUnit 4 test suite with BeforeClass and AfterClass methods that make
Let's say you have some 3rd-party library class that you want to extend, simply
Possible Duplicate: differences between 2 JUnit Assert classes I have two junit-4.8.1.jars in my
We have a JUnit test suite that we usually run from Eclipse. We have
Is it possible to JUnit test if wiring by Spring is succesfully? I would
with ant it is possible to run JUnit tests and generate test reports in
Possible Duplicate: Choose order to execute JUnit tests I am writing a JUnit test.
Is it possible to set env vars in pom.xml that junit tests uses? Netbeans

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.