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

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

工欲善其事,必先利其器。使用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>

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小南家的青蛙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值