当前位置: 首页 > news >正文

使用 PowerMockito 模拟 new A() 行为

使用 PowerMockito 模拟 new A() 行为

在 Java 测试中,当你想模拟 new A() 实例化的对象行为时,可以使用 PowerMockito。下面是一个完整的示例:

完整实现方案

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;import static org.junit.Assert.assertEquals;// 1. 使用PowerMockRunner
@RunWith(PowerMockRunner.class)
// 2. 准备要修改字节码的类
@PrepareForTest(YourClass.class) // 包含对new A()调用的类
public class YourClassTest {@Mockprivate A mockA; // Mock对象@Testpublic void testYourMethod() throws Exception {// 3. 设置mock对象的返回值Mockito.when(mockA.count()).thenReturn(15); // 模拟返回15// 4. 模拟new A()操作PowerMockito.whenNew(A.class).withNoArguments().thenReturn(mockA);// 5. 创建被测类实例YourClass yourClass = new YourClass();// 6. 执行测试int result = yourClass.yourMethod();// 7. 验证结果assertEquals(15, result);// 可选:验证方法是否被调用Mockito.verify(mockA).count();}
}class A {public int count() {return 0; // 实际实现}
}class YourClass {public int yourMethod() {A a = new A(); // 需要被模拟的实例化return a.count();}
}

Maven 依赖配置

<dependencies><!-- JUnit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!-- Mockito Core --><dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>5.3.1</version><scope>test</scope></dependency><!-- PowerMock --><dependency><groupId>org.powermock</groupId><artifactId>powermock-module-junit4</artifactId><version>2.0.9</version><scope>test</scope></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-api-mockito2</artifactId><version>2.0.9</version><scope>test</scope></dependency>
</dependencies>

关键步骤解析

  1. 测试类配置​:

    • @RunWith(PowerMockRunner.class):使用PowerMock运行器
    • @PrepareForTest(YourClass.class):指定需要修改字节码的类
  2. 模拟构造函数​:

    PowerMockito.whenNew(A.class)   // 拦截对A类的实例化.withNoArguments()           // 使用无参构造函数.thenReturn(mockA);          // 返回模拟对象
  3. 设置方法返回值​:

    Mockito.when(mockA.count())  // 当调用count()方法时.thenReturn(15);      // 返回指定值

实际效果说明

yourMethod() 中的 new A() 执行时:

  1. PowerMock 会拦截构造函数调用
  2. 返回预先创建的 mockA 实例
  3. 调用 count() 方法时返回预设值 (15)

这样既不需要修改被测试类的代码,也能完全控制新创建对象的行为。

注意事项

  1. 测试效率​:PowerMock 修改字节码,会使测试执行变慢
  2. 兼容性​:确保 PowerMock 版本与 Mockito/JUnit 版本兼容
  3. 替代方案​:优先考虑重构代码使用依赖注入
  4. 范围限制​:只能模拟 new 表达式直接出现的类

对于简单场景,推荐优先重构代码使用依赖注入。但当处理遗留代码或第三方库时,PowerMock 提供的这种能力非常有用。

http://www.lryc.cn/news/589057.html

相关文章:

  • 文心一言开源版部署及多维度测评实例
  • linux-线程互斥
  • 硬件设计学习DAY1——电源的分类
  • HAProxy 简介及配置文件详解
  • nlp论文:分本分类:《Bag of Tricks for Efficient Text Classification》
  • 渭河SQL题库-- 来自渭河数据分析
  • 5.数据归一化
  • Python爬虫实战:研究Mistune库相关技术
  • UE5多人MOBA+GAS 23、制作一个地面轰炸的技能
  • Typecho插件开发:实现文章字数统计与阅读时长计算功能
  • Docker镜像导入、导出操作指南
  • 大型语言模型(LLM)的技术面试题
  • 如何通过 WebSocket 接口订阅实时外汇行情数据(PHP 示例)
  • 深入探讨Hadoop YARN Federation:架构设计与实践应用
  • CentOS 8-BClinux8.2更换为阿里云镜像源:保姆级教程
  • Linux、Ubuntu和CentOS的关系与区别
  • RNN、GRU 与 LSTM 计算成本深入对比
  • 贪心贪心的反悔
  • 大语言模型零样本情感分析实战:无需机器学习训练,96%准确率实现指南
  • 003大模型基础知识
  • QT——文件选择对话框 QFileDialog
  • Perfectly Clear WorkBench V4.6.1.2731图像后期处理调色工具安装部署
  • 3.2数据库-关系代数-函数依赖-范式
  • 深度强化学习 | 图文详细推导深度确定性策略梯度DDPG算法
  • linux网络编程之单reactor模型(二)
  • Web攻防-PHP反序列化字符逃逸增多减少成员变量属性解析不敏感Wakeup绕过
  • 第二章 数据的表示和运算
  • 【每天一个知识点】多模态信息(Multimodal Information)
  • 为何说分布式 AI 推理已成为下一代计算方式
  • AI-Compass LLM训练框架生态:整合ms-swift、Unsloth、Megatron-LM等核心框架,涵盖全参数/PEFT训练与分布式优化