Issue
I am trying to verify the static methods present in a class InOrder. Below is the sample class that is having a few static methods
public class SampleClass {
public static void staticMethod1(Param1 param)
{
//doSomething
}
public static void staticMethod2(Param2 param)
{
//doSomething
}
public static void staticMethod2(Param3 param)
{
//doSomething
}
}
public class parent{
public void method() {
SampleClass.staticMethod1(...);
SampleClass.staticMethod2(...);
SampleClass.staticMethod3(...);
}
}
And my test looks like this
@Test
public void test(){
try (MockedStatic<SamplClass > mockSamplClass = mockStatic(SamplClass .class, Answers.CALLS_REAL_METHODS))
{
//doStuff
mockSampleClass.verify(()->SampleClass.staticMethod1(...));
mockSampleClass.verify(()->SampleClass.staticMethod2(...));
mockSampleClass.verify(()->SampleClass.staticMethod3(...));
}
}
Is there any way to perform InOrder verification for the three static methods? As Mockito.inOrder takes in only mock types and not MockedStatic types.
Right now I am kind of looking for answers in Mockito not involving PowerMockito.
Solution
For those that found this question as the 1st result on Google like me, now Mockito supports this. Using your example:
@Test
public void test(){
try (MockedStatic<SamplClass > mockSamplClass = mockStatic(SamplClass.class, Answers.CALLS_REAL_METHODS)) {
//doStuff
InOrder inOrder = Mockito.inOrder(SamplClass.class);
inOrder.verify(mockSampleClass, ()-> SampleClass.staticMethod1(...));
inOrder.verify(mockSampleClass, ()-> SampleClass.staticMethod2(...));
inOrder.verify(mockSampleClass, ()-> SampleClass.staticMethod3(...));
}
}
For more info see the Mockito github issue
Answered By - user1912694
Answer Checked By - Cary Denson (JavaFixing Admin)