交付进度紧张,为什么还要花费精力写单元测试代码,如下是之前的条记。
工欲善其事,必先利其器。使用Java语言交付项目时,需要掌握单元测试框架和Mock工具的使用。
如下是当年交付项目过程中,使用过的测试框架和工具,可供参考。
JUnit
测试用例的开发框架,从JUnit3不停到JUnit4。
JUnit5发布后,岗位的变革,在项目里写代码的机会不多,因此没有太多使用经验。
使用JUnit5时,修改项目的pom.xml,增加如下配置:- <dependency>
- <groupId>org.junit.jupiter</groupId>
- <artifactId>junit-jupiter-api</artifactId>
- <version>5.10.3</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.junit.jupiter</groupId>
- <artifactId>junit-jupiter-engine</artifactId>
- <version>5.10.3</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.junit.jupiter</groupId>
- <artifactId>junit-jupiter</artifactId>
- <version>5.10.3</version>
- <scope>test</scope>
- </dependency>
复制代码 使用JUnit4时,修改项目的pom.xml,增加如下配置:- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.13.2</version>
- <scope>test</scope>
- </dependency>
复制代码 参考资料
EasyMock
当年作为骨干加入祖传项目的交付,使用EasyMock配合PowerMock,写了很多单元测试,对于稳固产品质量,改善生活质量,非常有资助。
- 官网
- 代码仓库
EasyMock, makes mocking easier since 2001.
EasyMock is a Java library that provides an easy way to use Mock Objects in unit testing.
- Getting Started
- 使用指导
修改项目的pom.xml,增加如下配置:- <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,增加如下配置:- <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-core</artifactId>
- <version>2.0.9</version>
- <scope>test</scope>
- </dependency>
复制代码 官方文档
参考资料
JMockit
刚入行的时候,隔壁项目组大规模应用本工具交付单元测试和集成测试代码,其时作为新手,除了哇塞,也不知道说啥好。
修改项目的pom.xml,增加如下配置:- <dependency>
- <groupId>org.jmockit</groupId>
- <artifactId>jmockit</artifactId>
- <version>1.49</version>
- <scope>test</scope>
- </dependency>
复制代码 参考资料
Mockito
曾经在一个项目中使用过,不外这个项目的生命周期比较短,以是积聚不多。
修改项目的pom.xml,增加如下配置:- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-core</artifactId>
- <version>5.12.0</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-junit-jupiter</artifactId>
- <version>5.12.0</version>
- <scope>test</scope>
- </dependency>
复制代码 参考资料
Cucumber
场景化测试、数据驱动测试,之前加入网络控制器的业务时,广泛使用了本工具来开发单元测试和集成测试代码,非常方便。
- 官网
- 社区主页
Cucumber is a tool for running automated acceptance tests, written in plain language.
- 代码仓库
Cucumber for the JVM.
- 官方文档
修改项目的pom.xml,增加如下配置:- <dependency>
- <groupId>io.cucumber</groupId>
- <artifactId>cucumber-java</artifactId>
- <version>7.18.1</version>
- </dependency>
- <dependency>
- <groupId>io.cucumber</groupId>
- <artifactId>cucumber-junit</artifactId>
- <version>7.18.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>io.cucumber</groupId>
- <artifactId>cucumber-core</artifactId>
- <version>7.18.1</version>
- </dependency>
复制代码 参考资料
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |