PowerMockito在超类静态方法上给出了InvalidUseOfMatchersException

时间:2014-09-30 16:24:43

标签: java unit-testing junit mockito powermock

我试图模仿这种静态方法:

public abstract class Model {
   public static <Type> Type find(Class<Type> modelClass, Object id) {
     // some code
   }
}

从扩展类中调用

public static class Post extends Model {
}

使用此测试用例

@PrepareForTest(Post.class)
@RunWith(PowerMockRunner.class)
public class PostEditorControllerTest {
  mockStatic(Post.class);
  when(Post.find(eq(Post.class), eq(99))).thenReturn(this.post);
}

测试以org.mockito.exceptions.misusing.InvalidUseOfMatchersException失败,但我猜匹配器是正确的。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

应该是

mockStatic(Model.class);
when(Post.find(eq(Post.class), eq(99))).thenReturn(this.post);