【发布时间】:2021-03-26 15:14:56
【问题描述】:
当我尝试从某个 Google 库中模拟某些最终类(例如 SearchGoogleAdsRequest)时,他们给了我 IllegalAccessError ,我在类顶部添加了 @PrepareForTest 注释。
@RunWith(PowerMockRunner.class)
@PrepareForTest({SearchGoogleAdsRequest.class})
@PowerMockIgnore({"javax.net.ssl.*"})
public class GoogleAdsReportDownloaderTest {
final SearchGoogleAdsRequest searchGoogleAdsRequest = PowerMockito.mock(SearchGoogleAdsRequest.class);
final GoogleAdsServiceClient mockGoogleAdsServiceClient = mock(GoogleAdsServiceClient.class);
final GoogleAdsServiceClient.SearchPagedResponse searchPagedResponse =
mock(GoogleAdsServiceClient.SearchPagedResponse.class);
when(mockGoogleAdsServiceClient.searchPagedCallable()).thenReturn(callable);
when(mockGoogleAdsServiceClient.searchPagedCallable().call(searchGoogleAdsRequest)).thenReturn(searchPagedResponse);
when(searchPagedResponse.iterateAll()).thenReturn(Arrays.asList(mockGoogleAdsRow));
when(mockGoogleAdsServiceClient.search(any())).thenReturn(searchPagedResponse);
错误
java.lang.IllegalAccessError: Class com/google/ads/googleads/v6/services/SearchGoogleAdsRequest$MockitoMock$1353664588 illegally accessing "package private" member of class com/google/protobuf/GeneratedMessageV3$UnusedPrivateParameter
SearchGoogleAdsRequest final 类看起来像这样,PowerMockito 无法模拟。
public final class SearchGoogleAdsRequest extends GeneratedMessageV3 implements SearchGoogleAdsRequestOrBuilder {
private static final SearchGoogleAdsRequest DEFAULT_INSTANCE = new SearchGoogleAdsRequest();
private static final Parser<SearchGoogleAdsRequest> PARSER = new AbstractParser<SearchGoogleAdsRequest>() {
public SearchGoogleAdsRequest parsePartialFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException {
return new SearchGoogleAdsRequest(input, extensionRegistry);
}
};
private SearchGoogleAdsRequest(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
this.memoizedIsInitialized = -1;
}
private SearchGoogleAdsRequest() {
this.memoizedIsInitialized = -1;
this.customerId_ = "";
this.query_ = "";
this.pageToken_ = "";
this.summaryRowSetting_ = 0;
}
我可以通过配置 MockMaker 绕过这个。但是 MockMaker 不能与 PowerMockito 一起使用并给出错误(无法初始化插件,并且某些加载程序应该与 Mockito 一起使用,但正在使用 PowerMockito 加载)。
我需要使用 Power Mockito,因为我需要模拟本地范围对象和其他人创建的其他单元测试与 MockMaker 冲突。
【问题讨论】:
标签: java mockito powermockito