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


相关推荐

  • 设置PHP最长运行时间

    设置PHP最长运行时间

    2021年10月27日
    47
  • [深度学习 – 实战项目] 行为识别——基于骨架提取/人体关键点估计的行为识别

    [深度学习 – 实战项目] 行为识别——基于骨架提取/人体关键点估计的行为识别行为识别——骨架提取/人体关键点估计我们可以通过深度学习,检测到一个人,但是那个人在做什么我们不知道。所以我们就想让神经网络既检测到人,又知道他在做什么。也就是对这个人的行为进行识别。一个人的行为可以有很多种,可以跑、跳、走、跌倒、打架……有一些我们可以看第一眼就知道他在干嘛,有些我们必须看一段才知道他在干嘛。所以我们要用神经网络来识别行为,就可以分成单帧图片的识别和连续帧图片的识别。如果是单帧图片的识别,例如举手、摆个姿势……等简单的动作,我们可以直接用卷积网络、或者直接用yolo进行训练。在数据集

    2022年6月21日
    51
  • Matlab求解非线性规划(fmincon函数的使用)[通俗易懂]

    Matlab求解非线性规划(fmincon函数的使用)[通俗易懂]最近写文章需要用到fmincon函数(寻找约束非线性多变量函数的最小值)做优化,于是抽空学习一下;按照惯例,继续开个博文记录一下学习的过程。

    2022年5月30日
    57
  • 004-protostuff踩坑-java bean新增字段反序列化失败问题

    004-protostuff踩坑-java bean新增字段反序列化失败问题protostuff避免更改java对象字段,比如新增一个,导致redis等缓存的数据反序列化失败问题??问题重现:我们有个方法通过attrKey查询List,同时方法中有缓存,会优先查询缓存,没有读库,然后写缓存返回。方法伪代码如下:publicvoidsetId(StringattrKey){//从缓存查询List<ConfAttr>attrValues=cacheClient.get(attrKey);

    2022年5月7日
    173
  • 海康威视 – 萤石云开放平台 js 版

    海康威视 – 萤石云开放平台 js 版

    2021年5月25日
    375
  • ksweb使用教程_html5websocket查改

    ksweb使用教程_html5websocket查改0x01介绍nim一键木马免杀项目,目前能完美过360、火绒等杀软。作者使用纯nim语言编写的shellcode记载器,代码基本是从之前c++版本平移过来,作者并没有提供部署教程,踩坑记录下。0x02使用教程环境:ubuntu20.4安装:#下载代码库cd/rootgitclonehttps://github.com/M-Kings/BypassAv-web.git #起个Ubuntu的docker安装do…

    2022年8月20日
    10

发表回复

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

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