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

Ala*_*Dea 8 java generics junit hamcrest junit4

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

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

这是注释类:

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();
}
Run Code Online (Sandbox Code Playgroud)

以及我应用该注释的类:

@SomeAnnotation(String.class)
public final class AnnotatedClass {
}
Run Code Online (Sandbox Code Playgroud)

发生类型错误是因为:

Class<?> value = AnnotatedClass.class.getAnnotation(SomeAnnotation.class).value();
Run Code Online (Sandbox Code Playgroud)

和:

Matcher<Class<String>> classMatcher = is(equalTo(String.class));
Run Code Online (Sandbox Code Playgroud)

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

<T> void assertThat(T, Matcher<? super T>)
Run Code Online (Sandbox Code Playgroud)

通过根据第一个参数修复T,更具体的是:

void assertThat(Class<?>, Matcher<? super Class<?>>)
Run Code Online (Sandbox Code Playgroud)

我喜欢assertThat的一致性,并且宁愿避免使用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());
    }
}
Run Code Online (Sandbox Code Playgroud)

如何匹配Class<?>Hamcrest匹配器中的特定类实例?

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

Sot*_*lis 12

一个相当丑陋的方法是使用原始类型.

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

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

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

(您不需要提取String.class变量.)