Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 863
swampsjohn Avatar asked Nov 23 '25 21:11

swampsjohn


1 Answers

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

like image 199
Egwor Avatar answered Nov 25 '25 09:11

Egwor