LambdaJ类属性匹配器

时间:2011-10-11 14:21:40

标签: java hamcrest lambdaj

我正在尝试编写一个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编写这样的匹配器?

1 个答案:

答案 0 :(得分:0)

你不需要lambdaj来实现这一点。可以使用instanceOf matcher来执行此操作:

assertThat(classHolder.getClazz(),Matchers.instanceOf(String.class)));