如何一起使用JUnit和Hamcrest?

我无法理解JUnit 4.8应该如何与Hamcrest匹配器一起工作。 org.hamcrest.CoreMatchers junit-4.8.jar中定义了一些匹配器。 同时, org.hamcrest.Matchers 其他一些matcher也在hamcrest-all-1.1.jar中。 那么,去哪里? 我应该明确地将Hamcrest JAR包含到项目中,而忽略JUnit提供的匹配器吗?

特别是,我对empty()匹配器感兴趣,并且无法在这些jar中find它。 我需要别的东西? 🙂

还有一个哲学问题:为什么JUnit将org.hamcrest包纳入自己的发行版,而不是鼓励我们使用原始的hamcrest库?

junit提供了新的检查断言方法,名为assertThat(),它使用了Matcher,并且应该提供更可读的testing代码和更好的失败消息。

为了使用这个,junit中包含了一些核心匹配器。 您可以从这些基本testing开始。

如果你想使用更多的匹配器,你可以自己写或使用最后的库。

以下示例演示如何在ArrayList上使用空匹配器:

 package com.test; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import java.util.ArrayList; import java.util.List; import org.junit.Test; public class EmptyTest { @Test public void testIsEmpty() { List myList = new ArrayList(); assertThat(myList, is(empty())); } } 

(我在我的构buildpath中包含了hamcrest-all.jar)

如果您使用的版本大于或等于1.2的Hamcrest,则应使用junit-dep.jar 。 这个jar没有Hamcrest类,因此你可以避免类加载问题。

从JUnit 4.11开始, junit.jar本身没有Hamcrest类。 不需要junit-dep.jar了。

不完全回答你的问题,但你一定要尝试FEST-Assertstream利的断言API。 它与Hamcrest竞争,但有一个更简单的API,只需要一个静态导入。 这是由cpater使用FEST提供的代码:

 package com.test; import java.util.ArrayList; import java.util.List; import org.junit.Test; import static org.fest.assertions.Assertions.assertThat; public class EmptyTest { @Test public void testIsEmpty() { List myList = new ArrayList(); assertThat(myList).isEmpty(); } } 

编辑:Maven坐标:

 <dependency> <groupId>org.easytesting</groupId> <artifactId>fest-assert</artifactId> <version>1.4</version> <scope>test</scope> </dependency> 

另外,如果正在使用JUnit 4.1.1 + Hamcrest 1.3 + Mockito 1.9.5,请确保不使用mockito-all。 它包含Hamcrest核心类。 使用mockito-core代替。 下面的configuration工作:

 <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>1.9.5</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>hamcrest-core</artifactId> <groupId>org.hamcrest</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.1.1</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>hamcrest-core</artifactId> <groupId>org.hamcrest</groupId> </exclusion> </exclusions> </dependency> 

由于版本一直在变化,所以我发布信息让大家知道截至2014年12月2日, http://www.javacodegeeks.com/2014/03/how-to-test-dependencies-in -a-maven-project-junit-mockito-hamcrest-assertj.html为我工作。 我没有使用AssertJ,只是这些:

 <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.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> 

为什么JUnit将org.hamcrest包纳入了自己的发行版,而不是鼓励我们使用原始的hamcrest库?

我想这是因为他们想要断言这是JUnit的一部分。 这意味着Assert类必须导入org.hamcrest.Matcher接口,除非JUnit依赖于Hamcrest,或者包含(至less部分)Hamcrest,否则不能这样做。 我想包括它的一部分更容易,所以JUnit将是可用的,没有任何依赖。

根据相应的.xml文件,JUnit-4.12和JUnit-Dep-4.10都具有Hamcrest依赖性。

进一步的调查表明,虽然依赖是在.xml文件中,jar子里的源代码和类。 似乎是排除build.gradle中的依赖项的一种方法…testing它以保持一切清洁。

只是一个fyi