ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spock2.x结合mockito静态mock

2022-03-20 13:04:48  阅读:220  来源: 互联网

标签:Mockito spock2 mockito when CommonConstant getItem mock thenReturn


spock2.x开始已经移除了powermock,可以使用mockito-3.4.x之后的版本来解决

mock静态工具类

spock-1.x静态mock使用的是powermock,2.x之后可以结合Mockito-3.4及更新版本一起使用

pom.xml

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-core</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy</artifactId>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>4.3.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <version>4.3.1</version>
    <scope>test</scope>
</dependency>

mocktio静态示例

public class TestUtil {
    public static String getName(String name) {
        return "hello:" + name;
    }
}

junit5单元测试

@BeforeAll
public static void before() {
    MockedStatic<TestUtil> mockedStatic = Mockito.mockStatic(TestUtil.class);
}

@Test
@RepeatedTest(value = 3)
public void mockitoTest() {
    Mockito.when(TestUtil.getName("张三")).thenReturn("hello张三");
    Assertions.assertEquals(TestUtil.getName("张三"), "hello张三");

}

注意: RepeatedTest是重复执行多次,多个相同的静态方法按照官方示例执行一次不会报错,多次就报错了,所以使用Mockito.when().thenReturn()

spock中where示例

    @Shared
    MockedStatic spring
    @Shared
    MockedStatic helper

    def setupSpec() {
        spring = Mockito.mockStatic(SpringContext.class)
        helper = Mockito.mockStatic(Helper.class)
    }

    def check() {
        given:"初始化入参"
        def queue = new QueueCommon(1, 123)
        def queueCheck = new QueueCheck(id: id, auto: false)

        and:
        queueResultMapper.selectList(1, 123, _, null) >> new ArrayList()
        queueMapper.selectList(1, 123, _, null) >> listQueue
        queueMapper.updateStatus(*_) >> 1

        and:
        Mockito.when(Helper.getItem(Constant.QUEUE_STATUS,   "0101")).thenReturn(CommonConstant.getItem("0101"));
Mockito.when(Helper.getItem(Constant.QUEUE_STATUS,
"0102")).thenReturn(CommonConstant.getItem("0102"));
Mockito.when(Helper.getItem(Constant.QUEUE_STATUS, "0103")).thenReturn(CommonConstant.getItem("0103"));
Mockito.when(Helper.getItem(Constant.QUEUE_STATUS, "0201")).thenReturn(CommonConstant.getItem("0201"));
Mockito.when(Helper.getItem(Constant.QUEUE_STATUS,   "0202")).thenReturn(CommonConstant.getItem("0202"));
        
expect:
queueServiceImpl.check(queueCommonKey, queueCheck, getOperation(44805379620934L)) == true

        where:
        idQueue   || listQueue | listQueueResult
        123||  Arrays.asList(CommonConstant.getQueueById(123)) | Arrays.asList(CommonConstant.getQueueResultById(123))
        456||  Arrays.asList(CommonConstant.getQueueById(456)) | Arrays.asList(CommonConstant.getQueueResultById(456))
    }

def cleanupSpec() {
    spring.close()
    helper.close()
}

标签:Mockito,spock2,mockito,when,CommonConstant,getItem,mock,thenReturn
来源: https://www.cnblogs.com/javashare/p/16029609.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有