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

论坛 期权论坛 脚本     
匿名技术用户   2021-1-8 17:54   37   0

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 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP