【问题标题】:Unable to mock certain final classes with PowerMockito - java.lang.IllegalAccessError无法使用 PowerMockito 模拟某些最终类 - java.lang.IllegalAccessError
【发布时间】: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


    【解决方案1】:

    我使用 newBuilder 创建类的对象并绕过该问题解决了这个问题。它并没有真正模拟课程,但我可以在我的案例中使用它作为我需要模拟的课程的参数。如果我们不需要 PowerMockito,那么我们可以将 Mockito 与 MockMaker 一起使用,但是这些类可以很容易地模拟。

    final SearchGoogleAdsRequest searchGoogleAdsRequest = SearchGoogleAdsRequest.newBuilder().build();
    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);
    
    
     
    

    【讨论】:

      猜你喜欢
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      相关资源
      最近更新 更多