Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I call Apache's MethodUtils methods with mocks from Mockito as paramaters?
    primarykey
    data
    text
    <p>I use <code>org.apache.commons.beanutils.MethodUtils</code> (<a href="http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/MethodUtils.html" rel="nofollow">api link</a>) on occasion. I generally avoid reflection, but alas this case called for it. </p> <p>My problem comes to testing. I use Mockito for mocking, and when I call <code>MethodUtils.invokeMethod</code> with a mock as an argument, MethodUtils throws a <code>NoSuchMethodError</code> because it thinks the mocked object doesn't match the parameter type. Any suggestions?</p> <p>Signature of concrete method I'm calling:</p> <pre><code>public void propagateMirrorRateOfferId(MasterAvailabilityRule masterAvailabilityRule) </code></pre> <p>My mock generation:</p> <pre><code>@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()); } } </code></pre> <h2>Thanks all, figured it out</h2> <p>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 <code>org.apache.commons.lang.reflect.MethodUtils</code> in my utility class. For some reason, that version of the class fails with the mocks. When I use <code>org.apache.commons.beanutils.MethodUtils</code> it works. Not sure why. Perhaps I should update my original question to ask why the one in the <code>reflect</code> package fails... Anyway, will mark my question as answered once enough time has passed for me to submit an answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload