那年追过的开发者测试工具

交付进度紧张,为什么还要花费精力写单元测试代码,如下是之前的笔记。

工欲善其事,必先利其器。使用Java语言交付项目时,需要掌握单元测试框架和Mock工具的使用。
如下是当年交付项目过程中,使用过的测试框架和工具,可供参考。

JUnit

测试用例的开发框架,从JUnit3一直到JUnit4。
JUnit5发布后,岗位的变化,在项目里写代码的机会不多,因此没有太多使用经验。

使用JUnit5时,修改项目的pom.xml,增加如下配置:

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>

使用JUnit4时,修改项目的pom.xml,增加如下配置:

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

参考资料

EasyMock

当年作为骨干参与祖传项目的交付,使用EasyMock配合PowerMock,写了很多单元测试,对于稳固产品质量,改善生活质量,非常有帮助。

修改项目的pom.xml,增加如下配置:

<!-- https://mvnrepository.com/artifact/org.easymock/easymock -->
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>

参考资料

PowerMock

对于老项目来说,静态类满天飞,PowerMock非常有用。
当年作为骨干参与祖传项目的交付,使用EasyMock配合PowerMock,写了很多单元测试,对于稳固产品质量,改善生活质量,非常有帮助。

检查提交记录,发现最近的提交已在2022年2月24日
除非有新的开发者加入,否则本项目短期内不会有新的进展。

  • 官网

  • 代码仓库

    PowerMock is a Java framework that allows you to unit test code normally regarded as untestable.

  • Is this project still alive?

    I don't personally have time to maintain PowerMock anymore, and from what I understand from @thekingn0thing, the same is true for him. Is anyone else willing to move it forward? I'd be happy to give access etc if anyone is willing to do so.

    依据帖子中作者@johanhaleby的上述答复,短期内可能不会重启项目的开发工作。

修改项目的pom.xml,增加如下配置:

<!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-core -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>

官方文档

参考资料

JMockit

刚入行的时候,隔壁项目组大规模应用本工具交付单元测试和集成测试代码,当时作为新手,除了哇塞,也不知道说啥好。

修改项目的pom.xml,增加如下配置:

<!-- https://mvnrepository.com/artifact/org.jmockit/jmockit -->
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.49</version>
<scope>test</scope>
</dependency>

参考资料

Mockito

曾经在一个项目中使用过,不过这个项目的生命周期比较短,所以积累不多。

修改项目的pom.xml,增加如下配置:

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>

参考资料

Cucumber

场景化测试、数据驱动测试,之前参与网络控制器的业务时,广泛使用了本工具来开发单元测试和集成测试代码,非常方便。

修改项目的pom.xml,增加如下配置:

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.18.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.18.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>7.18.1</version>
</dependency>

参考资料

posted @   jackieathome  阅读(85)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示