3

I have a class with methodA() that eventually calls a static methodB() that returns a String. methodA() can cause multiple invocations of methodB(), depending on the scenario.

I want to write a unit test to verify that a single invocation of methodA() will only result in a single invocation methodB().

How can this be done? I looked at PowerMockito but wasn't able to find suitable examples.

class classA {
   public int methodA() {
      // Do something that invokes a method that in turn calls B.methodB()
   }
}

class classB {
   public static String methodB(String str) {
      // Do something
   }
}
user1175969
  • 540
  • 6
  • 19

1 Answers1

0

To verify a static method call Mockito is not sufficient. You would need to use PowerMockito.

Before adding PowerMockito to your project I would suggest considering to change the static method and use a singleton with an instance method instead.

See this: How to verify static void method has been called with power mockito

Community
  • 1
  • 1
Tobb
  • 11,850
  • 6
  • 52
  • 77
  • Thanks, I am looking at `PowerMockito`. If possible, can you give an example. Would it be possible to do this without making the static method an instance method? – user1175969 Mar 15 '16 at 08:55
  • See the link. There are plenty of questions about this, yours is kind of a duplicate.. – Tobb Mar 15 '16 at 09:31
  • Interestingly, `verifyStatic(times(n))` is always true for me where `n` is any random +ve integer. – user1175969 Mar 15 '16 at 20:45