PowerMockito实战

单元测试

一、微服务单元测试

1、PowerMock相关说明网站

相关测试类型说明:

微服务架构下单元测试落地实践

Spring Boot单元测试

关于java 单元测试Junit4和Mock的一些总结

Java单元测试技巧之PowerMock

2、依赖

<!-- powermock 相关依赖 -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.7.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>1.7.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.8.9</version>
            <scope>test</scope>
        </dependency>
        <!--集成jaco co-->
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

    <!--jacoco插件-->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.3</version>
                <executions>
                    <execution>
                        <id>pre-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-test</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

3、powerMock测试

规避链路问题

  1. @RunWith(PowerMockrRunner.class)
  2. @InjectMocks: 创建需要关注的对象,可以被注入
  3. @Mock : 创建虚拟的对象,不关注的实际逻辑
  4. @Before:测试方法执行前执行
  5. when…A…thenRerun B 当执行A的时候返回值为B
  6. 右键文件Run test…caveged… 查看覆盖率

四、实战总结

1、BaseContextHandler

静态方法

解决

// 类上加上
@PrepareForTest({BaseContextHandler.class})

// 测试方法加上
@Before
public void before() {
    PowerMockito.mockStatic(BaseContextHandler.class);
    PowerMockito.when(BaseContextHandler.getUserId()).thenReturn("1");

}

2、MybatisPlusException

原因:Mybatis拿不到缓存EnvironmentCheckPO

LambdaUpdateWrapper<EnvironmentCheckPO> update = new LambdaUpdateWrapper();
        update.set(EnvironmentCheckPO::getIsDelete, 1).in(EnvironmentCheckPO::getId, split);
        this.update(update);

报错 EnvironmentCheckPO

com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not find lambda cache for this property [isDelete] of entity [cn.com.hatechframework.server.comparison.po.EnvironmentCheckPO]

解决方式 EnvironmentCheckPO

TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(),""),EnvironmentCheckPO.class);

3、使用MP继承(实现)中的方法

原因

this.update(update);
——————————————————————————————————————————
com.baomidou.mybatisplus.extension.service.IService
public interface IService<T> {
    ……
default boolean update(Wrapper<T> updateWrapper) {
    return this.update((Object)null, updateWrapper);
	}
}

报错 空指针

解决

@PrepareForTest({IService.class})
public class EnvironmentCheckServiceImplTest {
    @Test
    public void deleteBatchByIds2() {
        Method saveBatch = PowerMockito.method(IService.class, "update", Wrapper.class);
        PowerMockito.replace(saveBatch).with((proxy, method, args) -> true);
        
    }
}

4、导入excel测试

文件位置

image-20210317135434239

模拟方式

@Test
    public void loadExcel3() {
        final InputStream inputStream = this.getClass().getResourceAsStream("/template/test/test-environmentcheck/environmentcheck-对比项.xlsx");
        final byte[] bytes = new byte[102400];
        try {
            inputStream.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        MockMultipartFile file = new MockMultipartFile("test", "test.xlsx", "xlsx", bytes);
        service.loadExcel(file);
    }

温馨提示:

  表格为空和表格无是两回事

表格为空:

image-20210317140303310

表格为无:没有任何边框和内容

image-20210328103810197

心得:

  像这种导入导出的单元测试,推荐,先写一个完全能跑通的excel表格,测试没问题后,后面的测试之用修改表格内容,和读取文件名称即可。

5、关于POI

坐标

  row=行  col =列

image-20210317142924710

image-20210317142648243

6、抛出异常

原方法

public ResponseObject<Object> downloadExcel(HttpServletResponse response){
        try {
            resourceService.downloadExcel(response);
            return ResponseResult.success();
        } catch (IOException ex) {
            log.info("下载模板异常",ex);
            return ResponseResult.error("操作失败");
        }
    }

抛出异常

 PowerMockito.doThrow(new IOException("Exception on purpose."))
                    .when(resourceService).downloadExcel(Mockito.any());

7、工具类静态方法抛出异常

工具类

public class FileUtils {
    // ……
    public static void copyInputStreamToFile(InputStream source, File destination) throws IOException {
            InputStream in = source;
            Throwable var3 = null;

            try {
                copyToFile(in, destination);
            } catch (Throwable var12) {
                var3 = var12;
                throw var12;
            } finally {
                if (source != null) {
                    if (var3 != null) {
                        try {
                            in.close();
                        } catch (Throwable var11) {
                            var3.addSuppressed(var11);
                        }
                    } else {
                        source.close();
                    }
                }

            }

        }
}

原方法

@Override
    public void generateEnterpriseIcon(String tenantId) {
        try (InputStream resourceAsStream = this.getClass().getResourceAsStream("/icon/defaultLogo.png");) {
            // ……
            FileUtils.copyInputStreamToFile(resourceAsStream, createFile);
            //……
        } catch (IOException e) {
            throw new BusinessException(ResponseCode.BUSINESS_ERROR.code(), "操作异常");
        }
    }

抛出异常

@PrepareForTest({FileUtils.class})
public class EnterpriseServiceImplTest {
    @Test(expected = BusinessException.class)
    public void generateEnterpriseIcon2() {
        PowerMockito.mockStatic(FileUtils.class);
        PowerMockito.doThrow(new IOException("单元测试模拟异常")).when(FileUtils.class);
        try {
            FileUtils.copyInputStreamToFile(Mockito.any(), Mockito.any());
        } catch (IOException e) {
            e.printStackTrace();
        }
        PowerMockito.when(tenantFileService.insert(Mockito.any())).thenReturn(1);
        service.generateEnterpriseIcon("1");
    }
}

8、注入配置文件属性

原方法

	/**
     * 文件上传根路径,最后包含/
     * D:/scene_upload/
     */
    @Value("${func.file.rootPath}")
    private String rootPath;

注入

@Before
public void before() {
    ReflectionTestUtils.setField(service, "rootPath", "/opt/xx/auth/func/file/");
}

10、区分系统

// 测试的时候区分系统
@Before
public void before() {
    // 区分系统
    String os = System.getProperty("os.name");
    if (os.toLowerCase().startsWith("win")) {
        ReflectionTestUtils.setField(service, "uploadPath", "D:\\unit-test\\");
    }
    if (os.toLowerCase().startsWith("linux")) {
        ReflectionTestUtils.setField(service, "uploadPath", "/opt/hatech/auth/func/file/");
    }
    System.out.println("当前系统" + os);
}

五、PO VO DTO 覆盖解决

1、添加插件

<!--jacoco插件——-->
<!--jacoco-->
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.3</version>
    <executions>
        <execution>
            <id>pre-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>post-test</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

2、提升sonar代码覆盖率,实体类单元测试覆盖率提升工具

把这ClassUtilEntityVoTestUtils放到java

ClassUtil

package cn.com.hatechframework.server;
import cn.com.hatechframework.config.exception.BusinessException;
import cn.com.hatechframework.utils.response.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
@Slf4j
public class ClassUtil {
    public static List<Class<?>> getClasses(String packageName) {
        ArrayList<Class<?>> classes = new ArrayList<>();
        ClassLoader classLoader = Thread.currentThread()
                .getContextClassLoader();
        String path = packageName.replace(".", "/");
        log.info("path:" + path);
        Enumeration<URL> resources;
        try {
            resources = classLoader.getResources(path);
        } catch (IOException e) {
            log.info("获取资源路径失败:" + e.getMessage(), e);
            return null;
        }
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            String protocol = resource.getProtocol();
            if ("file".equals(protocol)) {
                classes.addAll(findClasses(new File(resource.getFile()), packageName));
            } else if ("jar".equals(protocol)) {
                System.out.println("jar类型的扫描");
                String jarpath = resource.getPath();
                jarpath = jarpath.replace("file:/", "");
                jarpath = jarpath.substring(0, jarpath.indexOf("!"));
                classes.addAll(getClassListFromJarFile(jarpath, path));
            }
        }
        return classes;
    }
    private static List<Class<?>> findClasses(File directory, String packageName) {
        log.info("directory.exists()=" + directory.exists());
        log.info("directory.getName()=" + directory.getName());
        ArrayList<Class<?>> classes = new ArrayList<>();
        if (!directory.exists()) {
            return classes;
        }
        File[] files = directory.listFiles();
        if (files == null || files.length <= 0) {
            return classes;
        }
        for (File file : files) {
            if (file.isDirectory()) {
                assert !file.getName().contains(".");
                classes.addAll(findClasses(file,
                        packageName + "." + file.getName()));
            } else if (file.getName().endsWith(".class") && !file.getName().contains("Builder")) {
                try {
                    classes.add(Class.forName(packageName
                            + "."
                            + file.getName().substring(0,
                            file.getName().length() - 6)));
                } catch (Exception e){
                    throw new BusinessException(ResponseCode.BUSINESS_ERROR.code(), "类加载失败");
                }
            }
        }
        return classes;
    }
    /**
     * 从jar文件中读取指定目录下面的所有的class文件
     *
     * @param jarPath
     *            jar文件存放的位置
     * @param filePaht
     *            指定的文件目录
     * @return 所有的的class的对象
     */
    public static List<Class<?>> getClassListFromJarFile(String jarPath,  String filePaht) {
        List<Class<?>> clazzList = new ArrayList<>();
        JarFile jarFile = null;
        try {
            jarFile = new JarFile(jarPath);
            List<JarEntry> jarEntryList = new ArrayList<>();
            Enumeration<JarEntry> ee = jarFile.entries();
            while (ee.hasMoreElements()) {
                JarEntry entry = ee.nextElement();
                // 过滤我们出满足我们需求的东西
                if (entry.getName().startsWith(filePaht)
                        && entry.getName().endsWith(".class")) {
                    jarEntryList.add(entry);
                }
            }
            for (JarEntry entry : jarEntryList) {
                String className = entry.getName().replace('/', '.');
                className = className.substring(0, className.length() - 6);
                try {
                    clazzList.add(Thread.currentThread().getContextClassLoader()
                            .loadClass(className));
                } catch (ClassNotFoundException e) {
                    log.error("loadClass失败",e);
                }
            }
        } catch (IOException e1) {
            log.error("解析jar包文件异常");
        } finally {
            if (null != jarFile) {
                try {
                    jarFile.close();
                } catch (Exception e) {
                    log.error("关闭文件流失败",e);
                }
            }
        }
        return clazzList;
    }
}

3、实体类单元测试覆盖率提升工具

EntityVoTestUtils

@Slf4j
public class EntityVoTestUtils {
    //实体化数据
    private static final Map<String, Object> STATIC_MAP = new HashMap<>();
    //忽略的函数方法的method 
//    private static final String NO_NOTICE = "notify,notifyAll,wait,Builder";
    private static final String NO_NOTICE = "notifyAll,wait";

    static {
        STATIC_MAP.put("java.lang.Long", 1L);
        STATIC_MAP.put("java.lang.String", "test");
        STATIC_MAP.put("java.lang.Integer", 1);
        STATIC_MAP.put("int", 1);
        STATIC_MAP.put("long", 1);
        STATIC_MAP.put("java.util.Date", new Date());
        STATIC_MAP.put("char", '1');
        STATIC_MAP.put("java.util.Map", new HashMap());
        STATIC_MAP.put("boolean", true);
    }

    /**
     * 扫描实体类
     *
     * @param CLASS_LIST 类列表
     */
    public static void justRun(List<Class<?>> CLASS_LIST)
            throws IllegalAccessException, InvocationTargetException, InstantiationException {
        for (Class<?> temp : CLASS_LIST) {
            Object tempInstance = new Object();
            //执行构造函数
            for (Constructor constructor : temp.getConstructors()) {
                final Class<?>[] parameterTypes = constructor.getParameterTypes();
                // 无参数 调用无参构造
                if (parameterTypes.length == 0) {
                    tempInstance = constructor.newInstance();
                } else {
                    //有参数 调用有参构造
                    Object[] objects = new Object[parameterTypes.length];
                    for (int i = 0; i < parameterTypes.length; i++) {
                        objects[i] = STATIC_MAP.get(parameterTypes[i].getName());
                    }
                    tempInstance = constructor.newInstance(objects);
                }
            }
            //执行函数方法
            Method[] methods = temp.getMethods();
            for (final Method method : methods) {
                if (NO_NOTICE.contains(method.getName())) {
                    continue;
                }
                final Class<?>[] parameterTypes = method.getParameterTypes();

                if (parameterTypes.length != 0) {
                    Object[] objects = new Object[parameterTypes.length];
                    for (int i = 0; i < parameterTypes.length; i++) {
                        objects[i] = STATIC_MAP.get(parameterTypes[i].getName());
                    }
                    method.invoke(tempInstance, objects);
                } else {
                    method.invoke(tempInstance);
                }
            }
        }
    }
}

BeanUnitTesttest 目录下

@RunWith(PowerMockRunner.class)
public class BeanUnitTest {

    /**
     * 实体类的单元测试
     *
     * @throws IllegalAccessException    没有访问权限的异常
     * @throws InvocationTargetException 反射异常
     * @throws InstantiationException    实例化异常
     * @author daiweixing
     * @since 2021-2-10
     */
    @Test
    public void beanTest() throws IllegalAccessException, InvocationTargetException, InstantiationException {
        List<Class<?>> classes = ClassUtil.getClasses("cn.com.server");
        if (!CollectionUtils.isEmpty(classes)) {
            EntityVoTestUtils.justRun(classes.stream()
                    .filter(clazz -> (clazz.getName().contains(".vo.")
                            || clazz.getName().contains(".po.")
                            || clazz.getName().contains(".dto.")
                    ))
                    .collect(Collectors.toList()));
        }
    }
}

六、遇到的问题

问题一:

Lamda ——》@Data注解的问题

其中扫描的时候@Data无法被覆盖。会影响实体类的覆盖率

	解决方式
把@Data换成@Getter@Setter

问题二:

实体类有些方法无法覆盖

	能拿出去就拿出去
	实在不行直接写测试类给它覆盖吧!
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值