Posted on 周六 29 十月 2022

单元测试类库


修订历史

版本 日期 主要变更 作者
V1.2.0 2022-10-29 发布 潘力
V1.1.0 2022-10-21 发布 潘力
V1.0.0 2022-9-30 发布 潘力

参考文档

使用

类库发布在骏伯Nexus仓库
如需使用,请先添加仓库

repositories {
    maven {
        url "http://nexus.jetmobo.com/repository/Releases/"
        credentials {
            username "请向运维部申请"
            password "请向运维部申请"
        }
    }
}

然后添加引用

testImplementation 'info.panli:unittest:1.2.0'

//依赖的类库
testImplementation 'info.panli:kit:1.0.2'

描述

本类库主要提供一些常用测试类库的封装

info.panli.unittest.faker

模拟数据,基于java faker的封装。
如果使用请添加引用

testImplementation group: 'com.github.javafaker', name: 'javafaker', version: '1.0.2'
testImplementation group: 'org.jooq', name: 'joor-java-8', version: '0.9.14'

info.panli.unittest.junit5.params.provider

提供了对junit的参数化测试的支持。主要是ExcelSource和JsonConverter。
如果使用ExcelSource请添加引用

testImplementation group: 'org.apache.poi', name: 'poi', version: '4.1.2'
testImplementation group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'

如果使用JsonConverter请添加引用

testImplementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.13.3'
testImplementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.3'
testImplementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.13.3'

info.panli.unittest.mock

提供了对缓存、Redis、DB的mock

如果使用请添加引用

implementation group: 'org.mockito', name: 'mockito-core', version: '4.4.0'
implementation group: 'org.springframework.data', name: 'spring-data-redis', version: '2.0.8.RELEASE'
implementation group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: '2.8.0'
        Faker faker = ObjectFaker.get();
        //未指定TestInterface的实现类,会抛出异常
        Assertions.assertThatThrownBy(() -> faker.doFake(TestFakeData.class))
                .isInstanceOf(IllegalStateException.class);

        //指定TestInterface的实现类,正常模拟
        TestFakeData data = (TestFakeData) ObjectFaker.get()
                .continueWith(Utils.getConverter(InterfaceImplement.class, TestInterface.class))
                .doFake(TestFakeData.class).getData();

Links