Results 1 to 3 of 3
  1. #1
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Trying to test to see if an object is an instance of an interface class.

    I am currently trying to use Junit to test a whole bunch of stuff. I almost have full line coverage but I am getting hung up on testing an if statement that consists of whether or not an object is an instance of another class. This class happens to be an interface, and even the object is an interface. Weird I know but I just want to know how to get into that if statement. I realize testing interfaces might be a waste of time but I still really want to know how. Here is an example of what I am trying to test:
    Java Code:
    if(x instance of y){  //where x and y are both interface objects
    doSomething();
    }
    Last edited by Snake006; 03-07-2015 at 04:36 PM.

  2. #2
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: Trying to test to see if an object is an instance of an interface class.

    Is there some way I can force the object x to be an instance of class y for testing purposes?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Trying to test to see if an object is an instance of an interface class.

    You don't have to force anything. Just assign the instances like you normally would. Then for any instance x, use the following method

    Java Code:
    if (x.getClass().isInstance(obj o)) {
      // do something
    }
    Check the Class API for detailed documentation.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 08-20-2014, 07:17 PM
  2. check whether a object is interface or class
    By rajeev in forum New To Java
    Replies: 20
    Last Post: 08-26-2013, 05:27 AM
  3. Replies: 1
    Last Post: 07-08-2012, 06:23 PM
  4. Replies: 1
    Last Post: 04-21-2012, 07:03 PM
  5. Replies: 0
    Last Post: 10-03-2011, 09:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •