views:

65

answers:

1

Let's say I have a class

class SomeClass
{
  public void methodA()
  {}

  public void methodB()
  {}

  public void someMethod()
  {
     methodA();
     methodB();
  }
}

I would like to test behavior of someMethod() with Mockito.

The only way I could think of is using spy();

Something like

SomeClass someClass = spy(new SomeClass());
someClass.someMethod();
InOrder inOrder = inOrder(someClass);
inOrder.verify(someClass).methodA();
inOrder.verify(someClass).methodB();

I'm new to the mockito and documentation says

"Real spies should be used carefully and occasionally, for example when dealing with legacy code."

So maybe I'm missing something and there is better (right) way to verify that methodA and methodB were called without explicitly calling them in the test case.

Thanks.

A: 

Yes, spy() is fit for your purpose. The warning is due to the fact that real methods are invoked, and hence you can get unexpected results (for example - real money being withdrawn from a bank account)

Bozho