I have an aws lambda function as below:
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
I am writing a unit test for aws lambda using MockK. Following is the test class:
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)
}
When I run the test I get the following error:
io.mockk.MockKException: Can't instantiate proxy via default constructor for class FooHandler
When I looked further it also gave this error: Caused by: software.amazon.awssdk.core.exception.SdkClientException: Unable to load region from any of the providers in the chain software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain@7668d560
It looks like it is not mocking LambdaClient and is calling the actual function. Any ideas how can I reliably test my handler function.