无法使用 PowerMockito 模拟某些最终类

编程入门 行业动态 更新时间:2024-10-21 18:46:19
本文介绍了无法使用 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.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 模拟某些最终类

本文发布于:2023-10-31 04:49:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545052.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PowerMockito

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!