如何将类<! - ? - >与Hamcrest匹配器中的特定类实例进行匹配?

时间:2014-06-07 02:15:12

标签: java generics junit junit4 hamcrest

我希望能够断言注释值与预期的类匹配:

import org.junit.Test;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public final class AnnotatedClassTest {
    @Test
    public void someAnnotationIsString() {
        assertThat(
            AnnotatedClass.class.getAnnotation(SomeAnnotation.class).value(),
            is(equalTo(String.class));
    }
}

但是,这是一个类型错误:

AnnotatedClassTest.java:9: error: no suitable method found for assertThat(Class<CAP#1>,Matcher<Class<String>>)
        assertThat(
        ^
    method MatcherAssert.<T#1>assertThat(T#1,Matcher<? super T#1>) is not applicable
      (actual argument Matcher<Class<String>> cannot be converted to Matcher<? super Class<CAP#1>> by method invocation conversion)
    method MatcherAssert.<T#2>assertThat(String,T#2,Matcher<? super T#2>) is not applicable
      (cannot instantiate from arguments because actual and formal argument lists differ in length)
    method MatcherAssert.assertThat(String,boolean) is not applicable
      (actual argument Class<CAP#1> cannot be converted to String by method invocation conversion)
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>assertThat(T#1,Matcher<? super T#1>)
    T#2 extends Object declared in method <T#2>assertThat(String,T#2,Matcher<? super T#2>)
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?
1 error

这是注释类:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SomeAnnotation {
    Class<?> value();
}

我应用该注释的类:

@SomeAnnotation(String.class)
public final class AnnotatedClass {
}

发生类型错误是因为:

Class<?> value = AnnotatedClass.class.getAnnotation(SomeAnnotation.class).value();

Matcher<Class<String>> classMatcher = is(equalTo(String.class));

无法满足我打算定位的签名:

<T> void assertThat(T, Matcher<? super T>)

根据第一个参数修正 T 更具体的是:

void assertThat(Class<?>, Matcher<? super Class<?>>)

我喜欢断言的统一性,并希望避免使用 assertEquals

以下是如何使用 assertEquals (特别是不回答我的问题):

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public final class AnnotatedClassTest {
    @Test
    public void someAnnotationIsString() {
        assertEquals(
            String.class,
            AnnotatedClass.class.getAnnotation(SomeAnnotation.class).value());
    }
}

如何将Class<?>与Hamcrest Matcher中的特定Class实例进行匹配?

如果你能提供令人信服的解释,说明这是不可能的,这是一个可以接受的答案。

1 个答案:

答案 0 :(得分:12)

一种相当丑陋的方法是使用原始类型。

// this declaration is RAW
Class expected = String.class;
assertThat(AnnotatedClass.class.getAnnotation(SomeAnnotation.class)
                .value(), is(equalTo(expected)));

更好的方法是为equalTo(..)的调用指定泛型类型参数。

Class<?> expected = String.class;
assertThat(AnnotatedClass.class.getAnnotation(SomeAnnotation.class)
        .value(), is(CoreMatchers.<Class<?>>equalTo(expected)));

(您无需将String.class提取到变量中。)