Using 'stub' correctly
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 fubar
instance, which is under test, has nothing related to your stub.
I suggest you to use createPartialMock()
which allows you to create new instance of Fubar
and mock only bar()
method there. So this way you can test your fubar
instance (produced by createPartialMock()
) and record behavior of bar()
.
精彩评论