美文网首页Java单元测试
如何模拟返回值为void的静态方法

如何模拟返回值为void的静态方法

作者: SeanPenn | 来源:发表于2018-06-07 00:32 被阅读0次

      在编写代码时,经常需要调用别人已经写好的工具类方法,而这些方法经常又是使用静态方法实现的。而我们在测试自己的代码时,又不想真正执行这些方法,此时就需要对这些静态方法进行mock。在如何使用Powermock对静态方法进行mock文章中,我们介绍了如何对带有返回值的方法进行mock。那么对于返回值为void的方法如何模拟呢?
      同样的,我们先看下待测试类:

    public class Utility {
        public static void doSomething(String var) {
            throw new UnsupportedOperationException();
        }
    }
    
    public class UtilityHelper {
       public void call() {
           Utility.doSomething("test");
       }
    }
    
    1、不做任何方法模拟

      我们先不mock该静态方法,看下效果:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({Utility.class})
    public class UtilityHelperTest {
        private UtilityHelper utilityHelper;
    
        @Test(expected = UnsupportedOperationException.class)
        public void testCall() throws Exception {
            utilityHelper = new UtilityHelper();
            utilityHelper.call();
        }
    }
    

      执行该测试方法,会返回UnsupportedOperationException,正好和我们期望的结果相同,所以该测试用例执行正确。

    2、模拟静态类

      下面我们加一行代码,再看看效果:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({Utility.class})
    public class UtilityHelperTest {
        private UtilityHelper utilityHelper;
    
        @Test(expected = UnsupportedOperationException.class)
        public void testCall() throws Exception {
            PowerMockito.mockStatic(Utility.class);
    
            utilityHelper = new UtilityHelper();
            utilityHelper.call();
        }
    }
    

      由于我们加了一行对Utility类进行mock的语句,导致Utility里面的所有方法都不会被真正执行,此时调用doSomething方法时不会抛出UnsupportedOperationException,该测试方法执行错误。

    java.lang.AssertionError: Expected exception: java.lang.UnsupportedOperationException
    
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:318)
        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:300)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
        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:288)
        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:208)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:147)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:121)
        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:123)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
        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:68)
        at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    
    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
    
    3、去掉@Test后面的expected验证
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({Utility.class})
    public class UtilityHelperTest {
        private UtilityHelper utilityHelper;
    
        @Test
        public void testCall() throws Exception {
            PowerMockito.mockStatic(Utility.class);
    
            utilityHelper = new UtilityHelper();
            utilityHelper.call();
        }
    }
    

      此例中,由于我们去掉了expected这个期望验证结果,导致该测试方法目前没有任何验证存在,显然该用例执行正确。

    4、加个 doSomething方法被调用的验证

      没有任何验证的测试用例是无效的,下面我们在该用例中增加静态方法doSomething被调用的语句:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({Utility.class})
    public class UtilityHelperTest {
        private UtilityHelper utilityHelper;
    
        @Test
        public void testCall() throws Exception {
            PowerMockito.mockStatic(Utility.class);
    
            utilityHelper = new UtilityHelper();
            utilityHelper.call();
    
            PowerMockito.verifyStatic(Mockito.times(1));
            Utility.doSomething(Mockito.any());
        }
    }
    

      执行该测试用例,仍然执行正确,说明该静态方法确实是被调用了。此时我们可以看出,对于void类型的静态方法,我们不对该方法做任何的mock,仍然是执行正确的,这是因为mock整个类时,该类的所有方法就已经都被mock了,并且按照默认的方式进行处理,此时对于返回值为void的方法就是什么都不做。

    5、显示对void方法进行mock

      虽然不对void方法进行mock也能正常工作,但是我们还是习惯显示的对相应的方法进行mock,那么如何对void类型的方法进行mock呢?下面我们介绍两种写法:

    • 方法一
    PowerMockito.doNothing().when(Utility.class, "doSomething", Mockito.any());
    

      此处采取的when原型如下:

    <T> void when(Class<T> classMock, String methodToExpect, Object... parameters) throws Exception;
    

      函数注释如下,可见该种方式主要适用于对私有静态且无返回值的方法进行mock。对私有静态方法且有返回值的可以参考使用Powermock对私有方法进行mock

    Allows to mock a static private method based on method name and parameters when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style
    Example:
      doThrow(new RuntimeException()).when(MyClass.class, "methodName", parameter1, parameter2);

    • 方法二
    PowerMockito.doNothing().when(Utility.class);
    Utility.doSomething(Mockito.any());
    

      此处采取的when原型如下:

    void when(Class<?> classMock);
    

      函数注释如下,可见该种方式主要适用于对公有静态且无返回值的方法进行mock。

    Allows to choose a static method when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style
    Example:
      doThrow(new RuntimeException()).when(StaticList.class);
      StaticList.clear();

      综上所述,对于无返回值的静态方法,我们可以有两种方式实现mock:
      1) 隐式写法,即只需要mock静态类即可;
      2) 显示写法,此时需要根据方法是私有的还是公有的选择相应的格式;

    相关文章

      网友评论

        本文标题:如何模拟返回值为void的静态方法

        本文链接:https://www.haomeiwen.com/subject/hztwsftx.html