Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes any mocking framework allow spying on a super classes method?
    primarykey
    data
    text
    <p>We are using spring oauth and there are several places where we need to use inheritance.</p> <p>In the immediate case we are extending TokenEndpoint</p> <pre><code>public class MyTokenEndpoint extends TokenEndpoint { //... public ResponseEntity&lt;OAuth2AccessToken&gt; getAccessToken( Principal principal, MyParams myParams, @RequestParam Map&lt;String, String&gt; allParams) { // .. Stuff Happens updateParamsWithStuff(allParams); return super.getAccessToken( principal, myParams.grantType(), allParams); } //... } </code></pre> <p>Now what I want to test is if the map passed to super.getAcccessToken has been filled with Stuff. My simple way was to spy on the map passed in, but this relies on implemetation details and does not actually insure that stuff is in the map passed super.getAccessToken</p> <p>We are using Mockito, I have seen comments that this will not work, and one that implies it may. Can this be done in any of the mocking frameworks?</p> <p>See both answers on ( <a href="https://stackoverflow.com/questions/9957102/can-i-mock-a-superclasss-constructor-with-mockito-powermock">Can I mock a superclass&#39;s constructor with Mockito/Powermock?</a>, the checked one says impossible, but given the discussion on the second answer I just had to try.)</p> <p>After reading that I tried the following:</p> <pre><code>MyTokenEndpoint spyEndpoint = Mockito.spy(endpoint); //endpoint Set-up previously Mockito.doAnswer(new Answer&lt;ResponseEntity&lt;OAuth2AccessToken&gt;&gt;() { @Override public ResponseEntity&lt;OAuth2AccessToken&gt; answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Map&lt;String, String&gt; params = (Map&lt;String, String&gt;) args[2]; System.out.printf("%s\n", params.toString()); return new ResponseEntity&lt;OAuth2AccessToken&gt;(HttpStatus.ACCEPTED); } }).when(((TokenEndpoint) spyEndpoint)) .getAccessToken(any(Principal.class), anyString(), (Map&lt;String, String&gt;) anyMap()); theResponse = spyEndpoint .getAccessToken(principal, myPrams, currentMap); </code></pre> <p>But the code in answer never gets called.</p> <p>Am I barking up the wrong tree?? Is this possible in any mocking framework?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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