0

I have class

class CommandRunner {
  String output;

  public int run(String command) {
    //runs command and sets output         
    return errCode;
  }

  public String getOutput() {
    return output;
  }
}

Above CommandRunner is being in my class under test as

CommandRunner runner;
runner.run("some command");
out = runner.getOutput();
//operates on out

runner.run("some command");
out = runner.getOutput();
//operates on out

runner.run("some command");
out = runner.getOutput();
//operates on out

How can I mock CommandRunner to return different outputs based on input to run() method ? I using Power Mock with Mockito.

Sirish
  • 9,183
  • 22
  • 72
  • 107

1 Answers1

1

You could do this with a Mockito Answer, which is a chunk of code that is run when a method on a mock is called. You could write an Answer that checks the argument passed to the method, and returns something different in each case. Check out http://docs.mockito.googlecode.com/hg/latest/org/mockito/stubbing/Answer.html for how to do this.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110