如何使用 Mockito 中的模拟作为参数来调用 Apache 的 MethodUtils 方法?
我使用 org.apache.commons.beanutils.MethodUtils (api 链接)有时。我通常会避免反思,但可惜的是,这个案例需要反思。
我的问题在于测试。我使用 Mockito 进行模拟,当我使用模拟作为参数调用 MethodUtils.invokeMethod
时,MethodUtils 会抛出 NoSuchMethodError
因为它认为模拟对象与参数不匹配类型。有什么建议吗?
我调用的具体方法的签名:
public void propagateMirrorRateOfferId(MasterAvailabilityRule masterAvailabilityRule)
我的模拟生成:
@RunWith(MockitoJUnitRunner.class)
public class SomeTest {
private static final Long RATE_OFFER_ID = 1L;
private static final Long RATE_ENTITY_ID = 2L;
private static final Long ORIGINAL_RATE_OFFER_ID = 3L;
private static final Long ORIGINAL_RATE_ENTITY_ID = 4L;
@Mock MasterAvailabilityRule masterAvailabilityRule;
PropertyAvailabilityRule propertyAvailabilityRule;
@Before
public void setUp() throws Exception {
propertyAvailabilityRule = new PropertyAvailabilityRule();
propertyAvailabilityRule.setMirrorRateOfferId(ORIGINAL_RATE_OFFER_ID);
propertyAvailabilityRule.setMirrorRateEntityId(ORIGINAL_RATE_ENTITY_ID);
}
@Test
public void testPropagate() throws Exception {
// arrange
when(masterAvailabilityRule.determineAvailabilityMirroringRelationship(any(String.class))).
thenReturn(new AvailabilityMirroringRelationship(RATE_OFFER_ID, RATE_ENTITY_ID));
when(masterAvailabilityRule.isMirrorLocked()).
thenReturn(true);
// action
SomeUtility.thatCallsMethodUtilsAndPassesThisMock(masterAvailabilityRule);
// assert
assertEquals("Should have updated the mirrored rate offer id.", RATE_OFFER_ID, propertyAvailabilityRule.getMirrorRateOfferId());
assertEquals("Should have updated the mirrored rate entity id.", RATE_ENTITY_ID, propertyAvailabilityRule.getMirrorRateEntityId());
}
}
谢谢大家,想通了
在为你们提供示例时,我将 MethodUtils 调用直接放入我的测试中,并且它起作用了。事实证明,我已经从我的实用程序类中的 org.apache.commons.lang.reflect.MethodUtils 导入了 MethodUtils 类。由于某种原因,该版本的类因模拟而失败。当我使用 org.apache.commons.beanutils.MethodUtils 时它可以工作。不知道为什么。也许我应该更新我原来的问题来询问为什么 reflect
包中的问题失败...无论如何,一旦经过足够的时间让我提交答案,就会将我的问题标记为已回答。
I use org.apache.commons.beanutils.MethodUtils
(api link) on occasion. I generally avoid reflection, but alas this case called for it.
My problem comes to testing. I use Mockito for mocking, and when I call MethodUtils.invokeMethod
with a mock as an argument, MethodUtils throws a NoSuchMethodError
because it thinks the mocked object doesn't match the parameter type. Any suggestions?
Signature of concrete method I'm calling:
public void propagateMirrorRateOfferId(MasterAvailabilityRule masterAvailabilityRule)
My mock generation:
@RunWith(MockitoJUnitRunner.class)
public class SomeTest {
private static final Long RATE_OFFER_ID = 1L;
private static final Long RATE_ENTITY_ID = 2L;
private static final Long ORIGINAL_RATE_OFFER_ID = 3L;
private static final Long ORIGINAL_RATE_ENTITY_ID = 4L;
@Mock MasterAvailabilityRule masterAvailabilityRule;
PropertyAvailabilityRule propertyAvailabilityRule;
@Before
public void setUp() throws Exception {
propertyAvailabilityRule = new PropertyAvailabilityRule();
propertyAvailabilityRule.setMirrorRateOfferId(ORIGINAL_RATE_OFFER_ID);
propertyAvailabilityRule.setMirrorRateEntityId(ORIGINAL_RATE_ENTITY_ID);
}
@Test
public void testPropagate() throws Exception {
// arrange
when(masterAvailabilityRule.determineAvailabilityMirroringRelationship(any(String.class))).
thenReturn(new AvailabilityMirroringRelationship(RATE_OFFER_ID, RATE_ENTITY_ID));
when(masterAvailabilityRule.isMirrorLocked()).
thenReturn(true);
// action
SomeUtility.thatCallsMethodUtilsAndPassesThisMock(masterAvailabilityRule);
// assert
assertEquals("Should have updated the mirrored rate offer id.", RATE_OFFER_ID, propertyAvailabilityRule.getMirrorRateOfferId());
assertEquals("Should have updated the mirrored rate entity id.", RATE_ENTITY_ID, propertyAvailabilityRule.getMirrorRateEntityId());
}
}
Thanks all, figured it out
In coming up with the examples for you guys, I put the MethodUtils call directly in my test and it worked. Turns out the I had imported the MethodUtils class from org.apache.commons.lang.reflect.MethodUtils
in my utility class. For some reason, that version of the class fails with the mocks. When I use org.apache.commons.beanutils.MethodUtils
it works. Not sure why. Perhaps I should update my original question to ask why the one in the reflect
package fails... Anyway, will mark my question as answered once enough time has passed for me to submit an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我已经从我的实用程序类中的 org.apache.commons.lang.reflect.MethodUtils 导入了 MethodUtils 类。由于某种原因,该版本的类因模拟而失败。当我使用 org.apache.commons.beanutils.MethodUtils 时它可以工作。
Turns out the I had imported the
MethodUtils
class fromorg.apache.commons.lang.reflect.MethodUtils
in my utility class. For some reason, that version of the class fails with the mocks. When I useorg.apache.commons.beanutils.MethodUtils
it works.