I’m trying to test a method which calls a couple of other methods in the class. I’d like the other methods to be stubbed out so they don’t get executed. I had thought it was a simple matter of using ‘stub’. For example:
class Fubar {
void fu() {
// . . .
bar();
}
void bar() {
// . . .
}
void testFu() {
Fubar fubar = new Fubar();
stub (method (Fubar.class, "bar"));
replay();
fubar.fu();
verifyAll();
}
But this doesn’t seem to be working. It is terminating inside the ‘bar’ method when I had expected it to be basically a no-op. Am I using it incorrectly?
Thanks.
The principal problem of your approach is your
fubarinstance, which is under test, has nothing related to your stub.I suggest you to use
createPartialMock()which allows you to create new instance ofFubarand mock onlybar()method there. So this way you can test yourfubarinstance (produced bycreatePartialMock()) and record behavior ofbar().