Junit getAnnotation(Class<;T>;)在I';m使用EasyMock/PowerMock模拟java.lang.reflect.Method

Junit getAnnotation(Class<;T>;)在I';m使用EasyMock/PowerMock模拟java.lang.reflect.Method,junit,easymock,Junit,Easymock,测试方法具有以下代码: SuppressWarnings suppressWarnings = method.getAnnotation(SuppressWarnings.class); @RunWith(PowerMockRunner.class) @PrepareForTest(Method.class) public class AnnotationClassTest { @Test public void test() throws NoSuchMethodException,

测试方法具有以下代码:

SuppressWarnings suppressWarnings = method.getAnnotation(SuppressWarnings.class);
@RunWith(PowerMockRunner.class)
@PrepareForTest(Method.class)
public class AnnotationClassTest {
  @Test
  public void test() throws NoSuchMethodException, SecurityException {
    final Method mockMethod = PowerMock.createMock(Method.class);
    final Anot mockAnot = EasyMock.createMock(Anot.class);

    EasyMock.expect(mockMethod.getAnnotation(Anot.class)).andReturn(mockAnot);
    PowerMock.replay(mockMethod);

    final Anot methodReturn = mockMethod.getAnnotation(Anot.class);
    Assert.assertEquals(mockAnot, methodReturn);
  }
}

@Retention(RetentionPolicy.RUNTIME)
@interface Anot {}
在我的测试方法中,我模拟了java.lang.reflect.method:

Method method= PowerMock.createMock(Method.class);  
SuppressWarnings sw = EasyMock.createMock(SuppressWarnings.class);  
EasyMock.expect(method.getAnnotation(SuppressWarnings.class)).andReturn(sw);  
在测试方法中,
方法.getAnnotation(SuppressWarnings.class);始终返回null

我不知道为什么。有人能帮我吗

//code:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Anonymous {

}

public class AnnotationClass {
    public Anonymous fun(Method m){
        Anonymous anonymous = m.getAnnotation(Anonymous.class);
        return anonymous;
    }
}

// test class:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Method.class)
public class AnnotationClassTest {
    @Test
    public void test() throws NoSuchMethodException, SecurityException {
        AnnotationClass testClass = new AnnotationClass();
        final Method mockMethod = PowerMock.createMock(Method.class);
        final Anonymous mockAnot = EasyMock.createMock(Anonymous.class);

        EasyMock.expect(mockMethod.getAnnotation(Anonymous.class)).andReturn(mockAnot);
        PowerMock.replay(mockMethod);

        final Anonymous act = testClass.fun(mockMethod);
        Assert.assertEquals(mockAnot, act);

        PowerMock.verify(mockMethod);
    }
}

error:
java.lang.AssertionError: expected:<EasyMock for interface  
com.unittest.easymock.start.Anonymous> but was:<null>
//代码:
@保留(RetentionPolicy.RUNTIME)
@目标(ElementType.METHOD)
public@interface匿名{
}
公共类注释类{
公众匿名娱乐(方法m){
匿名=m.getAnnotation(Anonymous.class);
匿名返回;
}
}
//测试等级:
@RunWith(PowerMockRunner.class)
@PrepareForTest(方法类)
公共类注释ClassTest{
@试验
public void test()抛出NoSuchMethodException、SecurityException{
AnnotationClass testClass=新的AnnotationClass();
最终方法mockMethod=PowerMock.createMock(方法.class);
final匿名mockAnot=EasyMock.createMock(Anonymous.class);
expect(mockMethod.getAnnotation(Anonymous.class)).andReturn(mockAnot);
回放(mockMethod);
最终匿名act=testClass.fun(mockMethod);
Assert.assertEquals(mockAnot,act);
验证(mockMethod);
}
}
错误:
java.lang.AssertionError:应为:但为:
具有
@保留(value=SOURCE)
这意味着它在运行时不可用:

:编译器将丢弃注释

但是,如果您使用运行时可用的其他注释尝试代码,
method.getAnnotation(MyAnnotation.class)
仍将返回
null
。也就是说,因为默认情况下,模拟的
方法
对于方法调用将返回
null

我认为您的问题在于模拟的配置,当我运行您的代码(使用运行时可用的注释)时,会出现以下异常:

Exception in thread "main" java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:466)
at org.easymock.EasyMock.expect(EasyMock.java:444)
at MockStuff.main(MockStuff.java:54)
有一些关于如何模拟最终类的说明(例如
方法


你的代码给了我完全相同的结果。我能够使用以下代码使其工作:

SuppressWarnings suppressWarnings = method.getAnnotation(SuppressWarnings.class);
@RunWith(PowerMockRunner.class)
@PrepareForTest(Method.class)
public class AnnotationClassTest {
  @Test
  public void test() throws NoSuchMethodException, SecurityException {
    final Method mockMethod = PowerMock.createMock(Method.class);
    final Anot mockAnot = EasyMock.createMock(Anot.class);

    EasyMock.expect(mockMethod.getAnnotation(Anot.class)).andReturn(mockAnot);
    PowerMock.replay(mockMethod);

    final Anot methodReturn = mockMethod.getAnnotation(Anot.class);
    Assert.assertEquals(mockAnot, methodReturn);
  }
}

@Retention(RetentionPolicy.RUNTIME)
@interface Anot {}

请注意,此代码是自包含的,我定义了
Anot
接口,因为您没有给出
Anonymous

的定义。当我使用带有@Retention(RetentionPolicy.RUNTIME)的自定义注释时,我得到了相同的结果,我再次尝试,但是仍然失败。我在页面中键入了源代码。您能帮我解决这个问题吗?谢谢。是的,您更改的代码工作得很好。但是我需要在另一个方法中使用模拟方法类,就像我的代码一样,而不是直接使用方法类。