我正在尝试编写一个Hamcrest匹配器来验证返回class对象的方法的返回值。例如,给定类:
public static class ClassHolder {
private final Class clazz;
public ClassHolder(Class clazz) {
this.clazz = clazz;
}
public Class getClazz() {
return clazz;
}
}
我有一个测试,创建一个Hamcrest匹配器来匹配调用getClazz()
的结果:
public class LambdaJTest {
@Test
public void testClassHolder() {
final ClassHolder classHolder = new ClassHolder(String.class);
assertThat(classHolder, having(on(ClassHolder.class).getClazz(), is(String.class)));
}
}
但是,这个匹配器在执行时抛出异常:
ch.lambdaj.function.argument.ArgumentConversionException: It is not possible to create a placeholder for class: java.lang.Class
at ch.lambdaj.function.argument.ArgumentsFactory.createArgumentPlaceholder(ArgumentsFactory.java:170)
at ch.lambdaj.function.argument.ArgumentsFactory.createArgumentPlaceholder(ArgumentsFactory.java:156)
at ch.lambdaj.function.argument.ArgumentsFactory.createPlaceholder(ArgumentsFactory.java:52)
at ch.lambdaj.function.argument.ArgumentsFactory.registerNewArgument(ArgumentsFactory.java:45)
at ch.lambdaj.function.argument.ArgumentsFactory.createArgument(ArgumentsFactory.java:39)
at ch.lambdaj.function.argument.ProxyArgument.invoke(ProxyArgument.java:36)
at ch.lambdaj.proxy.InvocationInterceptor.intercept(InvocationInterceptor.java:42)
是否有一种方法来写这样一个匹配器使用LambdaJ?
您不需要lambdaj来实现这一点。可以通过使用instanceOf匹配器来实现,如下所示:
为了(classHolder.getClazz (), Matchers.instanceOf (String.class)));