I am trying to mock authentication and pass the mocked object while instantiating the child class
GoogleOAuthService mockGoogleOAuthService = Mockito.mock(GoogleOAuthService.class);
mockGoogleOAuthService = Mockito.spy(mockGoogleOAuthService);
GoogleCredentials mockGoogleCredentials = Mockito.mock(GoogleCredentials.class);
mockGoogleCredentials = Mockito.spy(mockGoogleCredentials);
Mockito.when(mockGoogleOAuthService.authenticateServiceAccount(secretJson, SERVICE_ACCOUNT_SCOPES)).thenReturn(mockGoogleCredentials);
final HotelViewTpaReportService hotelViewTpaReportService =
new HotelViewTpaReportService(mockGoogleOAuthService, dr);
Where the constructor of HotelViewTpaReportService
calls super
i.e public HotelViewTpaReportService(GoogleOAuthService googleOAuthService, DownloadRequest downloadRequest) throws Exception { super(googleOAuthService, downloadRequest);
And super class constructor then tries to authenticate googleOAuthService.authenticateServiceAccount
. It should at this point return the mocked mockGoogleCredentials
since we have written Mockito.when(mockGoogleOAuthService.authenticateServiceAccount(secretJson, SERVICE_ACCOUNT_SCOPES)).thenReturn(mockGoogleCredentials);
. But it doesn't and tries authenticating.
// Super Class Constructor
public AbstractTpaReportService(GoogleOAuthService googleOAuthService, DownloadRequest downloadRequest) throws Exception {
this.downloadRequest = downloadRequest;
this.tpaReportConfig = new TPAReportConfig(downloadRequest);
final byte[] secretJson = Base64.getDecoder().decode(getRequiredOption(downloadRequest, "secretJson"));
this.googleCredentials = googleOAuthService.authenticateServiceAccount(secretJson, SERVICE_ACCOUNT_SCOPES);
}
I am fairly new to Java , having migrated from python and nodejs.
Any help is appreciated.