-1

I got a method which includes a REST-request(Spring-Social) and i want to mock it. The thing is that i dont know how to access the call inside it or if it is possible at all. I got Mockito and PowerMock at my disposal

  private FacebookConnectionFactory facebookFactory;

  private OAuth2Operations authOps;

  private final OAuthCredentials credentials;

  ...

  public AccessToken exchangeAuthentication(String aCode) {
    facebookFactory = getOAuthConnectionFactory();
    authOps = facebookFactory.getOAuthOperations();
    authPar = new OAuth2Parameters();
    authPar.setRedirectUri(credentials.getFacebookRedirectURI());
    authPar.setScope("email");

    AccessGrant grant = authOps.exchangeForAccess(aCode, credentials.getFacebookRedirectURI(), null);  // I want mock this somehow

    AccessToken token = new AccessToken();
    token.setAccess_token(grant.getAccessToken());
    token.setExpires_in(grant.getExpireTime());
    token.setToken_type("bearer");

    return token;
  }
Maevy
  • 261
  • 4
  • 24

1 Answers1

0

You can use Mockito to create a mock from OAuth2Operations and stub the invocation you want:

@Mock
private OAuth2Operations authOps;

and after stub the exchangeForAccess invocation, like this:

when(authOps.exchangeForAccess(aCode, credentials.getFacebookRedirectURI(), null)).thenReturn(new AccessGrant());
Rafael Naufal
  • 411
  • 3
  • 6