rip*_*234 7 java junit unit-testing hamcrest
试图编译这段代码
import static org.hamcrest.Matchers.is;
import static org.hamcrest.number.OrderingComparison.lessThan;
...
Assert.assertThat(0, is(lessThan(1)));
Run Code Online (Sandbox Code Playgroud)
发出此编译错误:
assertThat(Object, org.hamcrest.Matcher<java.lang.Object>)无法应用(int, org.hamcrest.Matcher<capture<? super java.lang.Integer>>)
可能是不同的hamcrest版本之间的这种冲突?我正在使用jUnit 4.6和hamcrest 1.3
我认为问题在于JUnit捆绑了较旧版本的Hamcrest(1.1),因为Hamcrest的后续版本中的签名与JUnit不兼容.有两种可能的解决方案:
org.junit.Assert.assertThat()对org.hamcrest.MatcherAssert.assertThat()的调用.后者可能是我推荐的选项,因为Hamcrest版本assertThat()产生更好的失败消息,而1.1之后的版本有一些很好的功能(例如TypeSafeDiagnosingMatcher).
我不使用 Hamcrest,但显然int它不是一个对象。使用Integer代替,例如
Assert.assertThat(Integer.valueOf(0), is(lessThan(1)));
Run Code Online (Sandbox Code Playgroud)
我想您使用的是 Java 版本 <= 1.4,其中自动装箱不起作用。因此,您需要显式转换为Integer第一个。