Mockito(mockito-inline ^3.4.0) mockStatic 模拟无参、有参静态方法调用

Mockito早期一直不支持静态方法mock测试,需要借助PowerMockito实现静态方法mock测试。

自mockito 3.4.0开始,支持静态方法mock测试,用法简单方便。下面示例演示有参无参的静态方法调用mock测试和验证。

本文测试环境:

jdk11 + mockito4.0.0 + junit5 + spring-boot2.6.5

maven依赖:

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>

单测类示例:

package tom.demo.mockito;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.internal.invocation.InterceptedInvocation;
import org.mockito.internal.stubbing.answers.Returns;
import org.mockito.internal.verification.Times;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;

/**
 * 以前使用PowerMockito来mock静态方法,自Mockito 3.4.0 以后不再需要PowerMockito, Mockito本身已经支持
 * <br>
 * <code>https://javadoc.io/doc/org.mockito/mockito-core</code>
 */
public class MockStaticDemo {


    @Test
    void testStatic1() {
        MockedStatic<StaticDemo>  m = Mockito.mockStatic(StaticDemo.class);
        m.when(StaticDemo::hello).thenReturn("mock-hello").thenCallRealMethod();
        Assertions.assertEquals(StaticDemo.hello(), "mock-hello");
        Assertions.assertEquals(StaticDemo.hello(), "hello");
        m.verify(StaticDemo::hello, new Times(2));
        m.close();
    }

    @Test
    void testStatic2() {
        MockedStatic<StaticDemo>  m = Mockito.mockStatic(StaticDemo.class, new Returns("mock-returns"));
        Assertions.assertEquals(StaticDemo.hello(), "mock-returns");
        Assertions.assertEquals(StaticDemo.hi("tom"), "mock-returns");
        m.close();
    }

    @Test
    void testStatic3() {
        MockedStatic<StaticDemo>  m = Mockito.mockStatic(StaticDemo.class, invocation -> {
            Object[] args = invocation.getArguments(); // 调用方法的参数
            Object mock = invocation.getMock(); // 被mock的静态类的class信息,不是静态类本身
            Method method = invocation.getMethod(); // 被调用的具体方法
            String location = ((InterceptedInvocation) invocation).getLocation().toString(); // 调用具体位置
            System.out.println(location + " invoke method: " + method.getName()
                    + " with arguments: " + Arrays.toString(args)
                    + " return type: " + method.getReturnType().getName()
            );
            if (Objects.equals(method.getName(), "hello")) {
                return "mock-hello";
            } else if (Objects.equals(method.getName(), "hi")) {
                return "mock-hi" + Arrays.toString(args);
            }
            return location + " " + method.getName() + " called with arguments: " + Arrays.toString(args);
        });
        Assertions.assertEquals(StaticDemo.hello(), "mock-hello");
        System.out.println(StaticDemo.hi("cat"));
        System.out.println(StaticDemo.hi("dog"));
        System.out.println(StaticDemo.hi("dog"));
        System.out.println(StaticDemo.eat("elephant"));
        m.verify(StaticDemo::hello, new Times(1));
        m.verify(() -> {
            StaticDemo.hi("cat"); // 验证方法 hi() 调用参数cat 只调用了1次
        }, new Times(1));
        m.verify(() -> {
            StaticDemo.hi("dog"); // 验证方法 hi() 调用参数dog 只调用了2次
        }, new Times(2));
        m.verify(() -> {
            StaticDemo.hi(ArgumentMatchers.anyString()); // 验证方法 hi() 调用参数任意string串 调用了3次
        }, new Times(3));
        m.close();
    }


    static class StaticDemo {
        static String hello() {
            return "hello";
        }

        static String hi(String name) {
            return "hi " + name;
        }

        static String eat(String name) {
            return "eat " + name;
        }
    }

}

参考:

        mockito-core3.5.10

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值