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?

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

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!

This question has already been answered. Start a new discussion instead.