我希望能够断言注释值与预期的类匹配:
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<?>>)
我喜欢 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());
}
}
如何匹配 Class<?>
针对 Hamcrest 匹配器中的特定类实例?
如果您能提供令人信服的解释,则说明这是不可能的是一个可以接受的答案。
最佳答案
一种相当丑陋的方法是使用原始类型。
// 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
提取到变量中。)
关于java - 如何将 Class<?> 与 Hamcrest Matcher 中的特定 Class 实例进行匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24093000/