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?

Recommended Answers

All 4 Replies

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

post your code

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

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.