sin*_*ina 22 java junit hamcrest
考虑使用标准JUnit
断言和hamcrest的以下测试用例assertThat
:
byte b = 0;
int i = 0;
assertEquals(b, i); // success
assertThat(b, equalTo(i)); // java.lang.AssertionError: Expected: <0> but: was <0>
if (b == i) {
fail(); // test fails, so b == i is true for the JVM
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?对于JVM b == i
来说true
,这些值显然是相同的,因为为什么会hamcrest
失败?
Sot*_*lis 29
Assert#assertThat
是一种通用方法.原始类型不适用于泛型.在这种情况下,byte
和int
将分别加到Byte
和Integer
.
然后变成(内assertThat
)
Byte b = 0;
Integer i = 0;
b.equals(i);
Run Code Online (Sandbox Code Playgroud)
Byte#equals(Object)
的实现检查参数是否为类型Byte
,false
如果不是,则立即返回.
在另一方面,assertEquals
就是Assert#assertEquals(long, long)
在这种情况下,无论是byte
和int
参数都提升到long
价值观.在内部,它使用==
两个long
相等的原始值.
请注意,此拳击转换有效,因为assertThat
声明为
public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
Run Code Online (Sandbox Code Playgroud)
在哪里byte
被装箱为一个Byte
for T
,并且int
是一个盒装到一个Integer
(在通话中equalTo
),但推断为一个Number
匹配的Matcher<? super T>
.
这适用于Java 8改进的通用推理.您需要显式类型参数才能使其在Java 7中运行.
Mar*_*eel 13
之所以会发生这种情况int
,byte
是因为它们被装箱Integer
并且Byte
作为hamcrest匹配器在对象上操作,而不是在基元上操作.所以你要比较a Integer
和a Byte
的实现Byte.equals()
是:
public boolean equals(Object obj) {
if (obj instanceof Byte) {
return value == ((Byte)obj).byteValue();
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
并且Integer.equals()
:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
换句话说,一个Integer
并且Byte
总是不平等的.比较基元时,只需使用Assert.assertEquals
.hamcrest匹配器功能强大,但主要用于(复杂)对象断言.
归档时间: |
|
查看次数: |
2547 次 |
最近记录: |