سؤال

I am trying to write a Hamcrest matcher to verify the return value of a method that returns a Class object. For example, given the class:

public static class ClassHolder {
    private final Class clazz;

    public ClassHolder(Class clazz) {
        this.clazz = clazz;
    }

    public Class getClazz() {
        return clazz;
    }
}

I have a test that creates a Hamcrest matcher to match the result of calling 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)));
    }
}

However, this matcher throws an exception when executed:

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)

Is there a way to write such a matcher using LambdaJ?

هل كانت مفيدة؟

المحلول

You don't need lambdaj to achieve that. It's possible to do it by using the instanceOf matcher as it follows:

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top