3

How can I verify that the second static method was invoked within the first static method using the PowerMock framework?

public class A {
    public static int firstMethod(String s) {
        return secondMethod(s, 10);
    }

    public static int secondMethod(String s, Integer i){
        return /*some expression*/;
    }
}

Update:

I haven't see yet any solution using the PowerMock framework. And definitely there are no acceptable answers for me in the linked question.

Update2:

@Test
public void test() {
    PowerMockito.mockStatic(A.class);
    Mockito.when(A.secondMethod(Mockito.anyString(), Mockito.anyInt())).thenReturn(1000);
    A.firstMethod("test");
    PowerMockito.verifyStatic();
}
Community
  • 1
  • 1
barbara
  • 3,111
  • 6
  • 30
  • 58

1 Answers1

0

I think this is a bad design. You shouldn't HAVE to check internal details such as which methods are called.

But having said that, why not just make sure your first method returns the correct answer when the second method is mocked?

@Test
public void test() {
   PowerMockito.mockStatic(A.class);
   Mockito.when(A.secondMethod(Mockito.anyString(), Mockito.anyInt())).thenReturn(1000);
   //1000 was returned by your mocked method
   assertEquals(1000, A.firstMethod("test") );
   PowerMockito.verifyStatic();
}

This assumes your code example is valid for your question where firstMethod delegates and returns the call from secondMethod

dkatzel
  • 31,188
  • 3
  • 63
  • 67