943,029 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 507
  • Java RSS
Feb 7th, 2010
0

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

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
iamsmooth is offline Offline
49 posts
since Jul 2009
Feb 8th, 2010
0
Re: Problem with accessing a field of a class that implements an interface
post your code, check with the instanceof operator in java what instance is actually residing in the ExitInterface type variable.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Feb 8th, 2010
0
Re: Problem with accessing a field of a class that implements an interface
post your code
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Kumar Sabnis is offline Offline
2 posts
since Apr 2009
Feb 8th, 2010
0
Re: Problem with accessing a field of a class that implements an interface
post your code
Hmm...wasn't that advice already given ?
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Feb 8th, 2010
0
Re: Problem with accessing a field of a class that implements an interface
Click to Expand / Collapse  Quote originally posted by verruckt24 ...
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!
Reputation Points: 10
Solved Threads: 0
Light Poster
iamsmooth is offline Offline
49 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC