我有一个aws lambda函数,如下所示:
class FooHandler: RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private val lambdaClient = LambdaClient.create()
private val logger = LogManager.getLogger(CollectorHandler::class)
override fun handleRequest(request: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent {
val response = APIGatewayProxyResponseEvent()
if (isRequiredHeaderEmpty(request, response) || isBodyEmpty(request, response)) {
return response
}
return response
我正在使用MockK编写aws lambda的单元测试。下面是测试类:
class FooHandlerTest {
private val request = APIGatewayProxyRequestEvent()
private var response = APIGatewayProxyResponseEvent()
@SpyK
private lateinit var handler: FooHandler
@MockK
private lateinit var lambdaClient: LambdaClient
@MockK
private lateinit var context: Context
@BeforeTest
fun setUp() {
handler = spyk()
lambdaClient = mockk()
context = mockk()
}
@Test
fun testHandleRequestWhenBodyIsEmpty(){
request.body=""
response = handler.handleRequest(request, context)
expectThat(response.statusCode).isEqualTo(HttpStatus.BAD_REQUEST)
}
当我运行测试时,我得到了以下错误:
io.mockk.MockKException:无法通过类FooHandler的默认构造函数示例化代理
当我进一步查看时,它也给出了以下错误:原因:软件。亚马逊。awssdk。核心。异常。SdkClientException:无法从链软件中的任何提供程序加载区域。amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain@7668d560
看起来它没有在模仿LambdaClient,而是在调用实际的函数。我知道如何可靠地测试我的处理函数吗?
1条答案
按热度按时间ddhy6vgd1#
我可以解决这个问题。我在Kotlin中使用strikt表示Assert,mockK表示mocking: