PowerMockito实战

PowerMockito实战单元测试一 依赖 powermock 相关依赖 dependency groupId org powermock groupId artifactId powermock module junit4 artifactId version 1 7 4 version scope test scope dependency

单元测试

一、微服务单元测试

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[]; 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 

问题二:

实体类有些方法无法覆盖

 能拿出去就拿出去 实在不行直接写测试类给它覆盖吧! 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233248.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java的遍历数组效率测试源码[通俗易懂]

    java的遍历数组效率测试源码[通俗易懂]packagejavatest;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;publicclassjavatest{ publicstaticvoidmain(String[]args){ Listlist=newArrayList(); l

    2025年11月23日
    6
  • POE交换机通用吗_工业级交换机

    POE交换机通用吗_工业级交换机目前PoE交换机需要量大,那一定有些人疑惑PoE交换机能够替代一般工业交换机应用吗?下面为大伙儿介绍下,一起来瞧瞧吧。一般状况下是还可以的,具备IEEE802.3af或是IEEE802.3at协议书的POE交换机,输出电压时会有一个小电流量侦测。假如另一方不是带PoE的设备,那么就不容易供以往48V的工作电压。可是PoE交换机虽具有交换机的作用,作为一般工业交换机应用时,沒有最大限度充分发挥它的使用价值,不足经济发展节省,是自然资源的消耗。假如不用对联接设备给予直流电,能够同时采用一般工业.

    2022年10月5日
    4
  • 心脏出血漏洞利用「建议收藏」

    心脏出血漏洞利用「建议收藏」0x0引言~心脏出血(英语:Heartbleed),也简称为心血漏洞,是一个出现在加密程序库OpenSSL的安全漏洞,该程序库广泛用于实现互联网的传输层安全(TLS)协议。它于2012年被引入了软件中,2014年4月首次向公众披露。只要使用的是存在缺陷的OpenSSL实例,无论是服务器还是客户端,都可能因此而受到攻击。此问题的原因是在实现TLS的心跳扩展时没有对输入进行适当验证(缺少边界检查),…

    2022年7月17日
    12
  • 云服务器和虚拟主机的区别是什么[通俗易懂]

    云服务器和虚拟主机的区别是什么[通俗易懂]通俗易懂一点来说,把云服务器比喻为一套房子,虚拟主机就是这间房子里的一个房间,群英网络建议大家他们两者的具体功能和区别可参考如下几点分析:1.性能不同:云服务器支持弹性扩展,按需付费,虚拟主机不支持,从稳定性和安全性来讲,云服务器要好些;2.权限不同:为防止资源浪费和受到攻击,虚拟主机开放权限较少,云服务器则没有这个问题,但搭建环境要麻烦些;3.配置环境:云服务器需手动配置环境,虚拟主机无需…

    2022年6月25日
    34
  • box-sizing:border-box的理解和作用

    box-sizing:border-box的理解和作用要想清楚这个属性的作用,首先要理解盒子模型盒子模型是指:外边距(margin)+border(边框)+内边距(padding)+content(内容)可以把每一个容器,比如div,都看做是一个盒子模型比如你给一个div设置宽高为500px,但实际你设置的只是content,之后你又设置了padding:10px;border:1pxsolidred;这时div的宽高就会变为544px(content500px+padding40px+border4px)相当于一个元素的实际宽高是由

    2025年7月17日
    5
  • 学习Java大数据需要掌握哪些Java技能?

    学习Java大数据需要掌握哪些Java技能?学习Java大数据需要掌握哪些Java技能?现在大数据发展很速度很多小伙伴想要学习Java大数据技术开发,但是学习大数据为什么需要掌握Java技能呢?一、学大数据为什么要掌握Java?首先,我们学习大数据,为什么要先掌握Java技术?Java是目前使用非常广泛的编程语言,它具有的众多特性,特别适合作为大数据应用的开发语言。Java不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的

    2022年5月27日
    31

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号