【发布时间】:2021-08-02 18:37:18
【问题描述】:
我正在尝试为下面的方法编写一个单元测试,它正在调用 Lambda。
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.lambda.LambdaClient;
import software.amazon.awssdk.services.lambda.model.InvokeRequest;
import software.amazon.awssdk.services.lambda.model.InvokeResponse;
public class RoutingService {
private LambdaClient lambdaClient;
private String env;
private String region;
public RoutingService(){
this.env = System.getenv(ENVIRONMENT);
this.region = System.getenv(REGION);
lambdaClient = LambdaClient.builder().build();
}
public RoutingResponse invokeLambda(InvokeLambda request) throws IOException {
RoutingResponse invokeResponse = new RoutingResponse();
String payload = createPayload(request); // Some utility method
String routingURI = getRoutingURI() ; // Another utility method
SDKBytes payloadBytes = SdkBytes.fromUtf8String(payload);
InvokeRequest invokeRequest = InvokeRequest.builder()
.functionName(routingUri)
.payload(payloadBytes)
.invocationType(INVOCATION_TYPE) // INVOCATION_TYPE is caonstant = RequestResponse
.build();
try{
InvokeResponse result;
result = lambdaClient.invoke(invokeRequest); // Was trying to Mock this
String resultString = result.payload().asUtf8String();
invokeResponse = new ObjectMapper().readValue(resultString, RoutingResponse.class);
}catch(AWSServiceExceptuion ex) {
logger.error(new ErrorEvent(ex.awsErrorDetails.errorMessage(), ex.getMessage(), routingUri));
invokeResponse.setStatusCode(ex.statusCode());
invokeResponse.setBody(ex.getMessage());
}
return response;
}
我正在尝试为该方法编写单元测试用例,如下所示(模拟 LambdaClient)。
但它仍然试图调用 Lambda 客户端的调用方法。
表示 Mocking 无法正常工作。
public class RoutingServiceTest {
@Mock
private LambdaClient lambdaClient;
@BeforeEach
void setup(){
MockitoAnnotations.initMocks(this);
}
@Test
void invokeLambda() throws IOException, JSONException {
Map<String,String> queryStringParameters = new HashMap<>();
queryStringParameters.put("test","GOOG");
String[] cookies = {"cookiea", "cookieb"};
Map<String,String> headers = new HashMap<>();
headers.put("header1","valuex");
headers.put("header2","valuey");
InvokeLambda fakeRequest = new InvokeLambda.InvokeLambdaBuilder()
.cdnToken("cdn-token").applicationName("eaf-4137-demo")
.service("quote").method("GET")
.path("QuoteService/getQuote")
.queryString(queryStringParameters)
.headers(headers)
.cookies(cookies)
.body("body")
.requestId("request-id")
.build();
InvokeResponse fakeResponse = InvokeResponse.builder().build(); // This class belongs to AWS library
Mockito.when(lambdaClient.invoke(Mockito.any(InvokeRequest.class))).thenReturn(fakeResponse); // This is not working correctly ..
RoutingService routingService = new RoutingService();
RoutingResponse response = routingService.invokeLambda(fakeRequest);
Assertions.assertTrue(response.getStatusCode()==200);
}
}
当我运行这个测试时,'RoutingService' 类的 try{} 块中抛出了一个异常,因为我们正在传递一个虚拟的“cdn 令牌” 在假响应中。 理想情况下,它不应该调用 lambda,因为我们正在模拟它。
请建议我在这里做错了什么? 如何纠正这个问题?
【问题讨论】:
-
请具体说明确切的错误是什么,以及您尝试解决的问题。
标签: java unit-testing aws-lambda mockito junit5