可以使用Mockito中的Micronaut无服务器功能测试中的Mock Beans
我是微守年的新手,我正在尝试几件事。我的最终目标是能够在AWS lambda上部署作为本机映像构建的Micronaut无服务器功能。
我已经完成了文档,但是,我正在努力在测试中嘲笑豆子。目前,我只是通过一个示例程序尝试此操作,我想从我的服务中嘲笑一个方法调用。在测试中使用Mockito时,我对模拟方法的响应无效。
package com.example;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import io.micronaut.context.ApplicationContext;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.function.aws.MicronautRequestHandler;
import jakarta.inject.Inject;
@Introspected
public class BookRequestHandler extends MicronautRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
@Inject
private BookService bookService;
public BookRequestHandler() {
}
public BookRequestHandler(ApplicationContext applicationContext) {
super(applicationContext);
}
@Override
public APIGatewayProxyResponseEvent execute(APIGatewayProxyRequestEvent request) {
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody(bookService.sayHello());
}
}
public interface BookService {
String sayHello();
}
@Singleton
public class BookServiceImpl implements BookService {
public String sayHello() {
return "HELLO";
}
}
@MicronautLambdaTest
public class BookRequestHandlerTest {
@Inject
private BookService bookService;
@Inject
private ApplicationContext applicationContext;
@Test
public void testHandler() {
BookRequestHandler bookRequestHandler = new BookRequestHandler(applicationContext);
when(bookService.sayHello()).thenReturn("MOCKED RESPONSE");
APIGatewayProxyResponseEvent responseEvent =
bookRequestHandler.execute(new APIGatewayProxyRequestEvent());
assertEquals(responseEvent.getStatusCode(), 200);
assertEquals(responseEvent.getBody(), "MOCKED RESPONSE");
}
@MockBean(BookServiceImpl.class)
BookService bookService() {
return mock(BookService.class);
}
}
如果有人可以在开发无服务器功能时,我可以将我指向嘲笑豆子的最佳实践,因为目前,我无法使用我的方法,我将非常感谢。
此外,在测试无服务器功能时建议使用@micronautlambdatest吗?在启动和AWS文档中生成的示例代码之间似乎存在一些差异。
任何帮助都非常感谢:D
I am super new to Micronaut and I am trying a couple of things out. My end goal is to be able to deploy a Micronaut Serverless Function, built as a Native Image on AWS Lambda.
I have gone through the documentation, however, I am struggling to mock Beans in my tests. Currently, I am only trying this out with a Sample Program where I want to mock out a method call from my service. I am getting a null response for my mocked method when using Mockito in tests.
package com.example;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import io.micronaut.context.ApplicationContext;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.function.aws.MicronautRequestHandler;
import jakarta.inject.Inject;
@Introspected
public class BookRequestHandler extends MicronautRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
@Inject
private BookService bookService;
public BookRequestHandler() {
}
public BookRequestHandler(ApplicationContext applicationContext) {
super(applicationContext);
}
@Override
public APIGatewayProxyResponseEvent execute(APIGatewayProxyRequestEvent request) {
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody(bookService.sayHello());
}
}
public interface BookService {
String sayHello();
}
@Singleton
public class BookServiceImpl implements BookService {
public String sayHello() {
return "HELLO";
}
}
@MicronautLambdaTest
public class BookRequestHandlerTest {
@Inject
private BookService bookService;
@Inject
private ApplicationContext applicationContext;
@Test
public void testHandler() {
BookRequestHandler bookRequestHandler = new BookRequestHandler(applicationContext);
when(bookService.sayHello()).thenReturn("MOCKED RESPONSE");
APIGatewayProxyResponseEvent responseEvent =
bookRequestHandler.execute(new APIGatewayProxyRequestEvent());
assertEquals(responseEvent.getStatusCode(), 200);
assertEquals(responseEvent.getBody(), "MOCKED RESPONSE");
}
@MockBean(BookServiceImpl.class)
BookService bookService() {
return mock(BookService.class);
}
}
I would really appreciate it if someone could point me towards the best practice for Mocking Beans in Micronaut when developing Serverless Functions because currently, I am not able to stub my methods.
Moreover, is using @MicronautLambdaTest recommended while testing Serverless Functions? There seems to be some discrepancy between the Sample Code generated from launch and the AWS Documentation.
Any help is much appreciated :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论