mercredi 30 décembre 2015

Mock and verify 2 methods public static void and public static boolean from same class using PowerMockito

I have a class say ClassA which has to 3-4 public static methods out of which my requirement is to mock 2 methods one is public static void and other is public static boolean.

Code snippet below

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

Now I have another class say ClassB has method1() calling ClassA.isConnected() and ClassA.doSomething() as below

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

Now I want to do unit testing for method1(), so I'm trying to mock isConnected() and doSomething() using PowerMockito, as below

@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.spy(ClassA.class);
    PowerMockito.doNothing().when(ClassA.class);
    ClassA.doSomething();
    //execute
    classB.method1();

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

ERROR:

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)

Aucun commentaire:

Enregistrer un commentaire