How do you stub an individual method in java for unit testing?

java, stub, unit-testing

Solution

Create a subclass that overrides bar() that does nothing.

Problem

I've got a method in a class that's writing to some string, which calls another method which does the same. Something like: ``` void foo() { a += "xyx"; bar(); } void bar() { a += "abc"; } ``` For unit testing purposes, I want to test foo and bar separately. Is there any way to prevent bar from being run when I call foo() or to replace it with another method?

Original source