Java 使用assertThat+;数据类型为short
我有一个类,有一个Java 使用assertThat+;数据类型为short,java,junit4,hamcrest,Java,Junit4,Hamcrest,我有一个类,有一个公共短状态,测试中有一行: assertThat(order.status, is(0)); 但它给了我以下错误: java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
公共短状态
,测试中有一行:
assertThat(order.status, is(0));
但它给了我以下错误:
java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.Assert.assertThat(Assert.java:923)
at com.example.OrderTest.testStuff(OrderTest.java:101)
...
但是,如果我执行以下更丑陋的操作,错误就会消失:
assertThat(order.status, is((short) 0));
怎么回事
assertThat()
的签名是assertThat(T actual,org.hamcrest.Matcher Matcher)
在你的例子中,T是:short
所以你需要一个匹配器。但是0是一个int文本。因此,您必须告诉编译器:将该int值用作short
尽管将0-int转换为0-short是完全正确的,但Java还不够聪明。当你将某些东西从整数范围“限制”到短程时,你必须放下该掷骰。是你的
顺序。状态是类型short?@Cargeh,如问题的第一行所述,是的@GhostCat但是当Java中没有任何short
文本时,它不应该工作吗?是不是应该去“好的,我们这里有一个short
和一个int
,我知道该怎么办”?除此之外,在我看来,这个问题是尽可能少、完整和可验证的……至于重复的问题,这个问题并没有回答我的问题。它回答了为什么Java中没有短文本。我的问题是为什么断言
和是
然后关心short和int之间的区别,而Java显然没有区别。Java不够聪明。。。在使用Java时,我总是缺少C