Results 1 to 3 of 3
- 03-07-2015, 04:30 PM #1
Senior Member
- Join Date
- Jun 2013
- Posts
- 129
- Rep Power
- 0
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.
- 03-07-2015, 04:44 PM #2
Senior Member
- Join Date
- Jun 2013
- Posts
- 129
- Rep Power
- 0
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?
- 03-07-2015, 06:24 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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 }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Jersey Test Framework - How to obtain instance of servlet/webresources and mock DAO?
By 012anonymousxyz in forum Web FrameworksReplies: 1Last Post: 08-20-2014, 07:17 PM -
check whether a object is interface or class
By rajeev in forum New To JavaReplies: 20Last Post: 08-26-2013, 05:27 AM -
Interface Inheritance : create class that implements the interface
By Zulkifli_Rahman in forum New To JavaReplies: 1Last Post: 07-08-2012, 06:23 PM -
Creating an instance variable in one class that connects to another instance variable
By SpicyElectricity in forum New To JavaReplies: 1Last Post: 04-21-2012, 07:03 PM -
How to return object or class from functions using Java Native Interface
By shai1981 in forum Advanced JavaReplies: 0Last Post: 10-03-2011, 09:27 AM
Bookmarks