网站建设 售后服务,制作网站视频,成都logo设计公司,宣传推广的十种方式maven mockito对于当今的大多数Java项目而言#xff0c;JUnit本身还远远不够。 您还需要一个模拟库#xff0c;也许还有其他东西。 在此迷你操作指南中#xff0c;我介绍了可以在新的Java项目中开始的测试依赖项。 一切都始于JUnit Maven存储库中的junit组中有两个工件JUnit本身还远远不够。 您还需要一个模拟库也许还有其他东西。 在此迷你操作指南中我介绍了可以在新的Java项目中开始的测试依赖项。 一切都始于JUnit Maven存储库中的junit组中有两个工件 junit和junit-dep 。 在4.9版之前后者不包含对内联的Hamcrest的依赖。 今天我们使用junit依赖关系如下 dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.11/versionscopetest/scope
/dependency dependency:tree产生 [INFO] \- junit:junit:jar:4.11:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test莫基托 我们通常需要的下一个依赖是一个模拟框架。 毫无疑问 Mockito是最受欢迎的游戏之一。 它有两个好处 mockito-all和mockito-core 。 第一个是将所有依赖项内联到其中的单个jar而后者只是Mockito。 建议将mockito-core与JUnit版本4.11一起使用。 因此我们添加依赖项 dependencygroupIdorg.mockito/groupIdartifactIdmockito-core/artifactIdversion1.9.5/versionscopetest/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:testHamcrest 知道mockito-core更适合于声明式依赖性管理因此我们将覆盖对Hamcrest和Objenesis的依赖性如下所示 dependencygroupIdorg.hamcrest/groupIdartifactIdhamcrest-core/artifactIdversion1.3/versionscopetest/scope
/dependencydependencygroupIdorg.objenesis/groupIdartifactIdobjenesis/artifactIdversion1.3/versionscopetest/scope
/dependency 有了这个我们可以轻松地添加Hamcrest库该库提供了一个匹配对象库依赖项 dependencygroupIdorg.hamcrest/groupIdartifactIdhamcrest-core/artifactIdversion1.3/versionscopetest/scope
/dependencydependencygroupIdorg.objenesis/groupIdartifactIdobjenesis/artifactIdversion1.3/versionscopetest/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的一个分支我前一段时间在这篇文章中写过。 那依赖性呢 让我们来看看 dependencygroupIdorg.assertj/groupIdartifactIdassertj-core/artifactIdversion1.5.0/versionscopetest/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 --
dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.11/versionscopetest/scope
/dependency
dependencygroupIdorg.mockito/groupIdartifactIdmockito-core/artifactIdversion1.9.5/versionscopetest/scope
/dependency
dependencygroupIdorg.assertj/groupIdartifactIdassertj-core/artifactIdversion1.5.0/versionscopetest/scope
/dependency
dependencygroupIdorg.hamcrest/groupIdartifactIdhamcrest-core/artifactIdversion1.3/versionscopetest/scope
/dependency
dependencygroupIdorg.hamcrest/groupIdartifactIdhamcrest-library/artifactIdversion1.3/versionscopetest/scope
/dependency
dependencygroupIdorg.objenesis/groupIdartifactIdobjenesis/artifactIdversion1.3/versionscopetest/scope
/dependency 您可以在GitHub上的unit-testing-demo项目中找到它链接到pom.xml 也可以尝试我的spring-mvc-quickstart-archetype 链接到pom.xml 。 参考操作方法在Codeleak.pl博客上从我们的JCG合作伙伴 Rafal Borowiec中测试Maven项目JUnitMockitoHamcrestAssertJ中的依赖项 。 翻译自: https://www.javacodegeeks.com/2014/03/how-to-test-dependencies-in-a-maven-project-junit-mockito-hamcrest-assertj.htmlmaven mockito