954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with accessing a field of a class that implements an interface

So let's say I have an interface called ExitInterface.java. Inside there is only one method declaration, exit().

I have two classes that implement this:
1) first one is named Exit.java which does System.exit(exitCode) in the exit method.
2) second one named ExitMock.java which doesn't exit, and instead keeps track of the exitCode in a field so it can be tested on in junit.

I can't seem to test the ExitMock from my program because the class that uses Exit has an instance of ExitInterface, which is normally initiated as Exit and when I'm testing I change it to initialize as ExitMock, but it won't let me access ExitMock's fields because it's not in ExitInterface. Even if I try wrapping it or adding some get/set methods, nothing works.

Does anyone know of a method I can fix this?

iamsmooth
Junior Poster in Training
50 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

post your code, check with the instanceof operator in java what instance is actually residing in the ExitInterface type variable.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

post your code

Kumar Sabnis
Newbie Poster
2 posts since Apr 2009
Reputation Points: 10
Solved Threads: 1
 
post your code

Hmm...wasn't that advice already given ?

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 
Hmm...wasn't that advice already given ?

Thanks for the help, but I found another solution was instead of using an interface to use the real class normally, but to use a class a subclass as the mock.

So basically ExitMock extends Exit, and it seems to work.

Appreciate the help guys!

iamsmooth
Junior Poster in Training
50 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You