1

由于嵌套抽象类,我无法模拟 AWS 翻译客户端

AWS Comprehend 代码链接

爪哇

public class TestLambdaFunctionHandler implements RequestHandler<InputPayLoadPojo, Response> {

AmazonTranslate awsTranslateClient = null;
AmazonComprehend comprehendClient = null;
awsTranslateClient = AmazonTranslateClientBuilder.standard().build();
comprehendClient = AmazonComprehendClientBuilder.standard().build();
...
} 

模拟课

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({ "javax.net.ssl.*" })
@PrepareForTest({AmazonComprehendClientBuilder.class, AmazonComprehendClientBuilder , AmazonTranslate.class, AmazonComprehend.class,})
public class SynchronousTest {

    @Before
    public void setup() {
    }

    @Test
    public void testSynchronousTranslation() {
        TaaSSyncLambdaFunctionHandler obj = Mockito.spy(new TaaSSyncLambdaFunctionHandler()); 

        AmazonTranslate awsTransMock = PowerMockito.mock(AmazonTranslate.class);
        AmazonComprehend awsComprehendMock = PowerMockito.mock(AmazonComprehend.class);
        AmazonTranslateClientBuilder awsTranslateClientBuilderMock = PowerMockito.mock(AmazonTranslateClientBuilder.class);
        AmazonComprehendClientBuilder awsComprehendClientBuilderMock = PowerMockito.mock(AmazonComprehendClientBuilder.class);
                                        
        when(awsTranslateClientBuilderMock.standard().build()).thenReturn(awsTransMock);
        when(awsComprehendClientBuilderMock.standard().build()).thenReturn(awsComprehendMock);
    ...
    }
}

错误

com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.
    at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:386)
    at com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:352)
    at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

注意:代码正在使用正确的凭据

我也尝试模拟抽象类,但我遇到了同样的错误。

4

0 回答 0