• Post Reply Bookmark Topic Watch Topic
  • New Topic

Isuue is Mocking Request Masterpass SDK "Token/create API"  RSS feed

 
Maneesh Tripathi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have Implemented Masterpass SDK in one of my Project for Checkout purpose.
The API is used to get request Token by passing the CallBackUrl and OrginURL
The API is implemented successfully and its working as expected.


Issue: I am facing issue with creating junit test cases using Mockito.

Code:
public void setupAndInvokeService(AuthTokenRequestBean requestBean) throws Exception {
retrieveAuthTokenService = new RetrieveAuthTokenService(requestBean);
when(processorFactory.createService(eq(RetrieveAuthTokenService.class),
  any(AuthTokenRequestBean.class))).
thenReturn(retrieveAuthTokenService);
Mockito.when(retrieveAuthTokenService.getOutput()).thenReturn(response);
Mockito.when(retrieveAuthTokenService.process(any(FlowCtx.class))).thenReturn(response);
}


Exception:

rg.mockito.exceptions.misusing.MissingMethodInvocationException:

when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.



Need help if anyone work on those apis for mocking using junit.
Is There any other Framework that can be used for mocking.

 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you're uisng this when statement:

However, you declared retrieveAuthTokenService above as:

This is not a mock, but rather just an actual instance of RetrieveAuthTokenService.
If you want to create a mock object, you must call the mock method. For example:

The same also might be happening with your processorFactory object - I don't know, because I don't see where it's declared here.
 
Maneesh Tripathi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you response.

public class RetrieveAuthTokenXXXXXX extends XXXX<AuthTokenResponseBean> {

public RetrieveAuthTokenXXXXXX(AuthTokenResponseBean retrieveAuthTokenRequest) {
this.authTokenRequest = retrieveAuthTokenRequest;
}

The class RetrieveAuthTokenService() is Constructor with Parameter class where i am passing request as a parameter.
I am facing challenge here to mock.
Tried with
final RetrieveAuthTokenProcessor retrieveAuthTokenProcessor =
   Mockito.spy(new RetrieveAuthTokenProcessor(request));
when(retrieveAuthTokenProcessor.fetchOAuthToken(ctx)).thenReturn("OAuth_Token");
but still in place of returning the "OAuth_Token" its going into the actual method call (ie. Actual call is happening)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!