【发布时间】:2021-03-09 06:26:48
【问题描述】:
我正在尝试模拟身份验证并在实例化子类时传递模拟对象
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);
HotelViewTpaReportService 的构造函数调用super 即public HotelViewTpaReportService(GoogleOAuthService googleOAuthService, DownloadRequest downloadRequest) throws Exception { super(googleOAuthService, downloadRequest);
然后超类构造函数尝试验证googleOAuthService.authenticateServiceAccount。此时它应该返回模拟的mockGoogleCredentials,因为我们已经写了Mockito.when(mockGoogleOAuthService.authenticateServiceAccount(secretJson, SERVICE_ACCOUNT_SCOPES)).thenReturn(mockGoogleCredentials);。但它没有并尝试进行身份验证。
// 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);
}
我是 Java 新手,从 python 和 nodejs 迁移过来。
感谢任何帮助。
【问题讨论】: