如何使用junit'检查列表是否只包含某些不相关的类类型;那是什么意思?
如果能得到hamcrest和junit matchers的帮助,我们将不胜感激……:) 我在Eclipse开普勒上使用junit-4.11.jar和hamcrest-core-1.3.jar,并使用sun的JDK1.6.030 我有一个类,它包含任何未知类型的实例,如下所示:如何使用junit'检查列表是否只包含某些不相关的类类型;那是什么意思?,junit,matcher,hamcrest,Junit,Matcher,Hamcrest,如果能得到hamcrest和junit matchers的帮助,我们将不胜感激……:) 我在Eclipse开普勒上使用junit-4.11.jar和hamcrest-core-1.3.jar,并使用sun的JDK1.6.030 我有一个类,它包含任何未知类型的实例,如下所示: class UnknownClassHolder { private Class<?> clazz; public Class<?> getClazz() { ret
class UnknownClassHolder {
private Class<?> clazz;
public Class<?> getClazz() {
return clazz;
}
public void setClazz(Class<?> clazz) {
this.clazz = clazz;
}
}
在org.hamcrest.matcherasert.assertThat(…)
方法中使用Matcher
是否有特殊原因
谢谢。首先,您应该使用
is
而不是isA
,因为您主张一个类等于另一个类<代码>isA用于测试对象是否是某个类的实例。其次,我唯一能做的就是强制编译器将它们视为原始Object
s
assertThat(u.getClazz(), anyOf(is((Object) Integer.class), is((Object) Vector.class)));
那个单独的
Collection.class
应该是Integer.class
?是的,这是一个打字错误。无论哪种方式,它都应该编译。。。我已经在原始版本中修复了它。不用担心,我想问题可能是Collection
和Vector
没有一个公共超类,但问题是David,谢谢你的回答。然而,您的解决方案并不能解决我的问题,因为您的测试在所有情况下都会通过。仅当列表具有整数
或向量
时,测试才应通过。如果,假设它有一个字符串
@hs76,当我将断言插入代码时,对于包含字符串的列表,断言正确失败,那么它应该失败。你测试过了吗?
The method assertThat(T, Matcher<? super T>) in the type Assert is not applicable for the arguments (Class<capture#1-of ?>, AnyOf<Vector>)
assertThat(u.getClazz().getName(), either(is(Integer.class.getName())).or(is(Vector.class.getName())));
assertThat(u.getClazz(), anyOf(is((Object) Integer.class), is((Object) Vector.class)));