EasyMock matcher for class data type

只愿长相守 提交于 2019-12-04 01:46:36

问题


I am having nightmares with the syntax for this and easymock:

public void foo(Class<?> clazz);

EasyMock.expects(object.foo(EasyMock.isA(???)));

What should I be putting if my argument is String.class? I initially thought:

EasyMock.isA(((Class<?>)(String.class)).getClass())

Yet when I make the call foo(String.class) I get:

java.lang.IllegalStateException: missing behavior definition for the preceding method call:


回答1:


You're attempting to verify a generic type that will be erased at runtime anyway.

Use a capture object instead:

Capture<Class<?>> classCapture = new Capture<Class<?>>();
EasyMock.expect(object.foo(EasyMock.capture(classCapture)));

// ... other test setup ...

Assert.assertEquals(classCapture.getValue(), String.class);



回答2:


I think the following will also work as an expect statement if you don't want to use a Capture:

EasyMock.expects(object.foo(EasyMock.isA(String.class.getClass())));


来源:https://stackoverflow.com/questions/9467049/easymock-matcher-for-class-data-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!