无法使用 PowerMockito 模拟某些最终类 - java.lang.IllegalAccessError [英] Unable to mock certain final classes with PowerMockito - java.lang.IllegalAccessError

查看:86
本文介绍了无法使用 PowerMockito 模拟某些最终类 - java.lang.IllegalAccessError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从某个 Google 库中模拟某些最终类,例如 SearchGoogleAdsRequest 时,他们给了我 IllegalAccessError ,我添加了 @PrepareForTest 注释在课堂上.

When I try to mock certain final classes like SearchGoogleAdsRequest from a certain Google library they give me IllegalAccessError , I have added @PrepareForTest annotation on top of the class.

@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 最终类看起来像这样,PowerMockito 无法模拟.

SearchGoogleAdsRequest final class looks like this, which PowerMockito isn't able to mock.

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 一起加载).

I am able to bypass this , by configuring MockMaker. But MockMaker doesn't work with PowerMockito and gives errors (cannot initialize plugin and that certain loader was supposed to go with Mockito but is loading with PowerMockito).

我需要使用 Power Mockito,因为我需要模拟本地作用域对象和其他人创建的其他单元测试与 MockMaker 中断.

I need to use Power Mockito because I need to mock local scope objects and other unit tests created by others break with MockMaker.

推荐答案

我使用 newBuilder 创建类的对象并绕过该问题解决了这个问题.它并没有真正模拟课程,但在我的情况下,我可以使用它作为我需要模拟的课程的参数.如果我们不需要 PowerMockito,那么我们可以将 Mockito 与 MockMaker 一起使用,但这些类可以轻松模拟.

I resolved this using newBuilder to create an object of the class and bypass the issue. It doesn't really mock the class, but I could use this in my case as an argument to a class which I needed to mock. If we don't require PowerMockito then we can use Mockito along with MockMaker, where these classes can be easily mocked though.

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);


 

这篇关于无法使用 PowerMockito 模拟某些最终类 - java.lang.IllegalAccessError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆