使用 powermock 和 return 一些基于参数的值模拟最终静态无效方法

Mocking final static void method using powermock and return some values based on the arguments

我试图在 final class 中模拟 final static void 方法。我想 return 一些值使用我最终方法中的参数。 我正在使用 powermockito。谁能告诉我我们如何模拟最终的静态无效方法和 return 我上面提到的一些值。

final class

public final class myFinalClass{

   public final static void myMethod(String s, String val) {

   }
}

PowerMockito 可用于模拟和验证静态方法。

如何模拟:

PowerMockito.mockStatic(ClassWithStaticMethods.class) 
PowerMockito.when(ClassWithStaticMethods.staticMethodCall()).thenReturn (obj1);

验证方式:

PowerMockotio.verifystatic(Mockito.times(1));
ClassWithStaticMethods.staticMethodCall();