maven mockito_如何:测试Maven项目(JUnit,Mockito,Hamcrest,AssertJ)中的依赖项

本文介绍了如何在Maven项目中进行单元测试,包括使用JUnit作为测试框架,Mockito作为模拟库,Hamcrest作为声明式匹配对象库,以及AssertJ提供丰富的断言。通过添加相应依赖,可以构建强大的测试环境。
摘要由CSDN通过智能技术生成

maven mockito

对于当今的大多数Java项目而言,JUnit本身还远远不够。 您还需要一个模拟库,也许还有其他东西。 在此迷你操作指南中,我介绍了可以在新的Java项目中开始的测试依赖项。

一切都始于JUnit

Maven存储库中的junit组中有两个工件: junitjunit-dep 。 在4.9版之前,后者不包含对内联的Hamcrest的依赖。 今天,我们使用junit依赖关系如下:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>

dependency:tree产生:

[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test

莫基托

我们通常需要的下一个依赖是一个模拟框架。 毫无疑问, Mockito是最受欢迎的游戏之一。 它有两个好处: mockito-allmockito-core 。 第一个是将所有依赖项内联到其中的单个jar,而后者只是Mockito。 建议将mockito-core与JUnit版本4.11一起使用。 因此,我们添加依赖项:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
</dependency>

现在, dependency:tree产生:

[INFO] +- junit:junit:jar:4.11:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] \- org.mockito:mockito-core:jar:1.9.5:test
[INFO]    \- org.objenesis:objenesis:jar:1.0:test

Hamcrest

知道mockito-core更适合于声明式依赖性管理,因此,我们将覆盖对Hamcrest和Objenesis的依赖性,如下所示:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
 
<dependency>
    <groupId>org.objenesis</groupId>
    <artifactId>objenesis</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

有了这个,我们可以轻松地添加Hamcrest库,该库提供了一个匹配对象库,依赖项:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
 
<dependency>
    <groupId>org.objenesis</groupId>
    <artifactId>objenesis</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

并且dependency:tree产生:

[INFO] +- junit:junit:jar:4.11:test
[INFO] +- org.mockito:mockito-core:jar:1.9.5:test
[INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] \- org.objenesis:objenesis:jar:1.3:test

断言

AssertJ – Java的流畅断言–提供了一组丰富而直观的强类型断言,可用于单元测试。 AssertJ是FEST Assert的一个分支,我前一段时间在这篇文章中写过。 那依赖性呢? 让我们来看看:

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>1.5.0</version>
    <scope>test</scope>
</dependency>

结果如下树:

[INFO] +- junit:junit:jar:4.11:test
[INFO] +- org.mockito:mockito-core:jar:1.9.5:test
[INFO] +- org.assertj:assertj-core:jar:1.5.0:test
[INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] \- org.objenesis:objenesis:jar:1.3:test

最终剪辑

完整的Maven结构如下所示:

<!-- Test -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>1.5.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.objenesis</groupId>
    <artifactId>objenesis</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

参考:操作方法:Codeleak.pl博客上从我们的JCG合作伙伴 Rafal Borowiec中测试Maven项目(JUnit,Mockito,Hamcrest,AssertJ)中的依赖项

翻译自: https://www.javacodegeeks.com/2014/03/how-to-test-dependencies-in-a-maven-project-junit-mockito-hamcrest-assertj.html

maven mockito

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值