那年追过的开发者测试工具
  s5EH7MiXPf4U 2024年08月10日 37 0

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

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

参考资料

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: Java IO 流详解 下一篇: ConcurrentHashMap的原理
  1. 分享:
最后一次编辑于 2024年08月10日 0

暂无评论

推荐阅读
  VGxawBTN4xmE   23小时前   10   0   0 Java
  FHUfYd9S4EP5   3天前   28   0   0 Java
  sSh3rBaiME5f   4天前   34   0   0 Java
  qCe06rFCa8NK   3天前   23   0   0 Java
  ZTo294hNoDcA   3天前   28   0   0 Java
  FHUfYd9S4EP5   3天前   22   0   0 Java
  QGiRIwDaZAe8   4天前   26   0   0 Java
s5EH7MiXPf4U