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


相关推荐

  • 剑指Offer面试题:11.调整数组顺序使奇数位于偶数前面建议收藏

    一题目:调整数组顺序使奇数位于偶数前面二解题思路如果不考虑时间复杂度,最简单的思路应该是从头扫描这个数组,每碰到一个偶数时,拿出这个数字,并把位于这个数字后面的所有数字往前挪动一位。挪完之后在

    2021年12月19日
    41
  • servlet和jsp中的Request转发,重定向有何区别_jsp重定向和转发的区别

    servlet和jsp中的Request转发,重定向有何区别_jsp重定向和转发的区别转发request.getRequestDispatcher("/index.jsp").forward(request,response);System.out.println("被执行了");index.jsp&lt;html&gt;&lt;body&gt;&lt;h2&gt;HelloWorld!&lt;/h2&gt;&lt;/body

    2025年10月2日
    1
  • 为什么没有小米7?[通俗易懂]

    为什么没有小米7?[通俗易懂]很多人会说小米手机没有小米7的原因,是因为7这个数字不是特别的好听。所以小米在进行第7代命名的时候将其进行了删除,所以你会发现没有小米7。但,在我看来,这种事实上并非是因为名字的问题,可能确实是因为其所代表的不是那么的舒适,不过在我看来还有一个非常重要的原因,可能就是因为小米8它刚好是小米8周年,也就是说小米7的推出实际上被改成小米8,就是印证了小米8周年的这个概念。刚好能够符合小米对于8周年的一个重视,而且这对于以后小米的10周年,小米10的出现,以及小米以后的20周年等等有更好的进行命.

    2022年4月19日
    254
  • html空格语言,html的空格代码是什么

    html空格语言,html的空格代码是什么html的空格代码是什么发布时间:2020-06-1716:00:48来源:亿速云阅读:253作者:元一html空格代码是为html空格字符代码,由“&+n+b+s+p+;”组成,记住最后一个分号不要忘记了。CSS中当white-space属性取值为pre时,浏览器会保留文本中的空格和换行,例如:AABB显示效果为:AABBCSS的letter-spacing属…

    2025年7月8日
    3
  • python 图像处理库_Python图像处理库

    python 图像处理库_Python图像处理库1.如果没有安装库就先安装库fromPILimportImage2.操作fromPILimportImage#导入库Image.open(‘1.png’)#读取图片im=np.array(Image.open(‘1.png’))#查看图片的gbk的颜色数组b=im[::-1]#改图片gbk数组的排列new_im=Image.fromarray(b.astype(‘uint8’))#

    2022年10月10日
    3
  • django模型数据类型_盒子模型的基本属性

    django模型数据类型_盒子模型的基本属性模型中常用字段字段说明AutoField一般不需要使用这个类型,自增长类型,数据表的字段类型为整数,长度为11位BigAutoField自增长类型,数据表的字段类型为bigint,长度为2

    2022年7月28日
    9

发表回复

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

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