使用PowerMockito模拟并验证来自同一类的2个方法public static void和public static boolean

时间:2015-12-30 11:42:05

标签: java android unit-testing mockito powermockito

我有一个课程说ClassA,其中有3-4个public static方法,我的要求是模拟2个方法,一个是public static void,另一个是public static boolean

下面的代码段

class ClassA{
   public static boolean isConnected(){
     //....
   }
   public static void doSomething(){
    //....
   }
  public static void doSomethingMore(){
    //....
   }
}

现在我有另一个班级说ClassBmethod1()来电ClassA.isConnected()ClassA.doSomething(),如下所示

class ClassB{
   public void method1(){
      if(ClassA.isConnected()){
        //...
      }else{
        ClassA.doSomething()
      }
   }
}

现在我想对method1()进行单元测试,因此我尝试使用isConnected()模拟doSomething()PowerMockito,如下所示

@RunWith(PowerMockRunner.class)
@PrepareForTest({ClassA.class})
public class ClassBTest{ 

@Test
public void testMethod1{
     //variables
     ClassB classB = new ClassB();
    //mock static non-void method
    PowerMockito.mockStatic(ClassA.class);
    PowerMockito.when(ClassA.isConnected()).thenReturn(false);
    //mock static void method
    PowerMockito.doNothing().when(ClassA.class);
    ClassA.doSomething();
    //execute
    classB.method1();

    //verify static non-void method called
    PowerMockito.verifyStatic(times(1)); 
    ClassA.isConnected();
    //Verify static void method called
    PowerMockito.verifyStatic(times(1)); 
    **ClassA.doSomething();**//getting error at this line
 }
}

错误:

Wanted but not invoked com.example.utils.common.ClassA.doSomething(
    );

However, there were other interactions with this mockcom.example.utils.common.ClassA.isConnected();

com.example.utils.common.ClassA.doSomethingMore(
    );

.
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:124)
    at org.powermock.core.MockGateway.methodCall(MockGateway.java:63)
    at com.example.utils.common.ClassA.doSomething(ClassA.java)
    at com.example.utils.common.ClassBTest.testMethod1(ClassBTest.java:177)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

1 个答案:

答案 0 :(得分:0)

作为一种替代方法来模拟静态正确。您可以修改遗留代码以提高可测试性。

以下示例保留静态方法,以便在代码库中的其他位置使用它们。显然,一旦删除了所有静态方法调用,你就可以从这里删除它们(然后除去instance)。

class ClassA{
   private static ClassA instance = new ClassA();

   public static boolean isConnected(){
     return instance.isConnected();
   }

   public boolean isConnected(){
     //....
   }

   public static void doSomething(){
    instance.doSomething();
   }

   public void doSomething(){
    //....
   }       

   public static void doSomethingMore(){
    instance.doSomethingMore();
   }

   public void doSomethingMore(){
    //....
   }
}

ClassB有一个setter,您可以根据代码将其更改为构造函数依赖项或注入框架。 setter受包保护,因此可见性最低。 annotation VisibleForTesting是可选的,只是突出了设置者的原因。

class ClassB{

   private ClassA classA = new ClassA();

   @VisibleForTesting
   void setClassA(ClassA classA) {
        this.classA = classA;
   }

   public void method1(){
      if(classA.isConnected()){
        //...
      }else{
        classA.doSomething()
      }
   }
}

无需PowerMock即可简化测试:

public class ClassBTest{ 

@Test
public void testMethod1{
     //variables
     ClassB classB = new ClassB();
     ClassA mockClassA = Mockito.mock(ClassA.class);
     classB.setClassA(mockClassA);
     // mock expected behaviour
     Mockito.when(mockClassA.isConnected()).thenReturn(false);

    //execute
    classB.method1();

    Mockito.verify(mockClassA, times(2)).isConnected();
    Mockito.verify(mockClassA).doSomething();
 }
}