Jli | 2022-11-02 01:50:40 UTC | #1
Hi, I am trying to use Junit and Mockit to test the code(①and②) under below , but it didn't work well. Do you know the right way to test the the method of ApiClient and FlowsApi using Junit and could you give some advices ,thank you !
→① ■Appication code ApiClient apiClient = ApiClient.Builder.standard().withBasePath(region).withAuthenticatedProxy(proxy,wuserId, wpass).build();
ApiResponse<AuthResponse> authResponse = apiClient.authorizeClientCredentials(clientId, clientSecret);
■Junit test code ApiClient apiClient1 = Mockito.mock(ApiClient.class);
Mockito.when(apiClient1.authorizeClientCredentials(Mockito.anyString(), Mockito.anyString())) .thenThrow(new IOException());
② ■Appication code FlowsApi apiInstance = new FlowsApi(); FlowAggregationQuery flowBody= new FlowAggregationQuery(); FlowAggregateQueryResponse result = apiInstance.postAnalyticsFlowsAggregatesQuery(flowBody);
■Junit test code FlowsApi mock = Mockito.mock(FlowsApi.class); Mockito.when(mock.postAnalyticsFlowsAggregatesQuery(Mockito.any(FlowAggregationQuery.class))).thenThrow(new IOException());
Thank you !
Jli | 2022-11-07 11:14:54 UTC | #2
Can anybody help me ? Any advice is welcome,thank you!
tim.smith | 2022-11-08 18:05:57 UTC | #3
I have no experience with Java testing, but I can point you to the tests that are used for testing the SDK package itself: https://github.com/MyPureCloud/platform-client-sdk-java/tree/master/build/src/test/java/com/mypurecloud/sdk/v2
Jli | 2022-11-10 09:24:41 UTC | #4
Thank you for you advice, but it seems I didn't explain my problem correctly.
What I want to do is to mock the under below class, then when I call one of the methods in my appication I can set the return value as I expect.
■ApiClient ApiClient apiClient1 = PowerMockito.mock(ApiClient.class); PowerMockito.whenNew(ApiClient.class).withAnyArguments().thenReturn(apiClient1); PowerMockito.when(apiClient1.authorizeClientCredentials(Mockito.anyString(),Mockito.anyString())).thenThrow(new IOException()); →Using PowerMockito,when I call <authorizeClientCredentials >method, I can set the resonse value like above as I expect.
My new problem is when I use PowerMockito to mock classes , it works well with ApiClient class above,but when I use it to mock the class following, it doesn't work.
Do you know what I am doing wrong,thank you !
■ConversationsApi ConversationsApi apiInstance = PowerMockito.mock(ConversationsApi.class); PowerMockito.whenNew(ConversationsApi.class).withNoArguments().thenReturn(apiInstance); PowerMockito.when(apiInstance.postAnalyticsConversationsAggregatesQuery(conversationAggregationQuery)).thenThrow(new IOException()); →I expected error but my application finished nomally. So it seems,the mock dind't work.
Any advice is welcome,thank you!
tim.smith | 2022-11-10 14:58:29 UTC | #5
I would suggest asking questions about how to use PowerMockito on their community forum if they have one, or a general purpose programming forum like stack overflow. Genesys does not provide support for 3rd party frameworks.
Jli | 2022-11-11 09:13:41 UTC | #6
I understand, thank you for the advice!
John_Carnell | 2022-11-15 16:15:18 UTC | #7
This post was migrated from the old Developer Forum.
ref: 17015