Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to write Unit Test for these APIs like KMS #605

Open
River-java opened this issue Jul 21, 2021 · 1 comment
Open

How to write Unit Test for these APIs like KMS #605

River-java opened this issue Jul 21, 2021 · 1 comment

Comments

@River-java
Copy link

River-java commented Jul 21, 2021

  • 产品和接口:
  • KMS
  • DecryptRequest
  • 平台:
  • 最小代码:
    KMS APS like below:
IClientProfile profile = DefaultProfile.getProfile(xxx, xxx, xxx);
DefaultAcsClient client = new DefaultAcsClient(profile)
DecryptRequest request = new DecryptRequest();
DecryptResponse response = client.getAcsResponse(request);

How can I write UT and no need to access Aliyun service?

@ljluestc
Copy link

ljluestc commented Sep 3, 2023

import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class MyKMSClassTest {

    @Mock
    DefaultAcsClient mockClient;

    @Mock
    DecryptResponse mockResponse;

    @InjectMocks
    MyKMSClass myKMSInstance;

    @Test
    public void testDecryptValue() throws Exception {
        when(mockClient.getAcsResponse(any(DecryptRequest.class))).thenReturn(mockResponse);
        when(mockResponse.getDecryptedValue()).thenReturn("DecryptedValue");

        String decryptedValue = myKMSInstance.decryptValue();

        assertEquals("DecryptedValue", decryptedValue);
        verify(mockClient, times(1)).getAcsResponse(any(DecryptRequest.class));
        verify(mockResponse, times(1)).getDecryptedValue();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants