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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • VMware 15 Pro 激活码 2021[在线序列号]

    VMware 15 Pro 激活码 2021[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    93
  • 求一个点在平面上的投影_unity获取物体坐标

    求一个点在平面上的投影_unity获取物体坐标Unity计算投影点主要内容新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML图表FLowchart流程图导出与导入导出导入主要内容本文旨在求得点在某一平面的投影点,主要包括Unity中InverseTransformPoint和TransformPoint方法。主要

    2025年8月12日
    2
  • linux 信号sigabrt,關於Linux中的SIGABRT信號

    SIGABRT是中止一個程序,它可以被捕捉,但不能被阻塞。處理函數返回后,所有打開的文件描述符將會被關閉,流也會被flush。程序會結束,有可能的話還會coredump。當程序調用abort(3)時,該進程會向自己發送SIGABRT信號。所以,SIGABRT一般用於信號中一些關鍵的處理,assert失敗時也會使用它。你不應該去捕捉SIGSEGV和SIGABRT信號,如果收到這種信號,說明進程處…

    2022年4月7日
    91
  • ubuntu杀死进程命令_ubuntu系统安装教程图解

    ubuntu杀死进程命令_ubuntu系统安装教程图解在用ubuntu的时候遇到几次程序卡死,但是不知道怎么关闭,心想有没有跟window一样的程序管理器存在?所以就去网上找了下解决方式记录一下:1、打开系统监视器:gnome-ststem-monitor然后就可以选择进程结束掉啦。2、如果知道程序名称可以命令杀死:killallXXX杀掉所有的进程killxxx(PID)杀死进程号xxx的进程,查看所有运行进程的命令:ps-aux…

    2022年9月21日
    2
  • lnmp一键安装的卸载

    lnmp一键安装的卸载

    2021年10月8日
    39
  • LeetCode – 23. Merge k Sorted Lists

    LeetCode – 23. Merge k Sorted Lists

    2021年9月12日
    50

发表回复

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

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