Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sats17/3962250684ef16d8b3f87b07fe247b91 to your computer and use it in GitHub Desktop.
Save sats17/3962250684ef16d8b3f87b07fe247b91 to your computer and use it in GitHub Desktop.
Mock Lambda Context and Logger while testing AWS Lambda RequestHandler (with JUnit4)
package com.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import com.amazonaws.services.lambda.runtime.ClientContext;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ssm.SsmClient;
/**
*/
public class JavaLambdaLocalTestAsJavaApplication {
public static void main(String[] args) throws FileNotFoundException {
MyLambdaFunction function = new MyLambdaFunction();
String putMsgBody = new Scanner(new File("src/test/resources/MyTestEvent.json")).useDelimiter("\\Z")
.next();
Context mockedContext = getContext();
function.handleRequest(putMsgBody, mockedContext);
}
private static Context getContext() {
Context context = new Context() {
@Override
public int getRemainingTimeInMillis() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getMemoryLimitInMB() {
// TODO Auto-generated method stub
return 0;
}
@Override
public LambdaLogger getLogger() {
// TODO Auto-generated method stub
return new LambdaLogger() {
@Override
public void log(byte[] message) {
System.out.println(message);
}
@Override
public void log(String message) {
System.out.println(message);
}
};
}
@Override
public String getLogStreamName() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getLogGroupName() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getInvokedFunctionArn() {
// TODO Auto-generated method stub
return null;
}
@Override
public CognitoIdentity getIdentity() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getFunctionVersion() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getFunctionName() {
// TODO Auto-generated method stub
return "local-order-management";
}
@Override
public ClientContext getClientContext() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getAwsRequestId() {
// TODO Auto-generated method stub
return "123456";
}
};
return context;
}
}
//In the test class
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.Mock;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.ArgumentMatchers.anyString;
@RunWith(MockitoJUnitRunner.class)//without this runner - mocks would be "null"
public class LambdaRequestHandlerTest {
private LambdaRequestHandler handler;
@Mock
Context context;
@Mock
LambdaLogger loggerMock;
@Before
public void setUp() throws Exception {
when(context.getLogger()).thenReturn(loggerMock);
doAnswer(call -> {
System.out.println((String)call.getArgument(0));//print to the console
return null;
}).when(loggerMock).log(anyString());
handler = new LambdaRequestHandler();
}
@Test
public void handleRequest() {
String respond = handler.handleRequest("some input", context);
}
}
//In the request handler
public class LambdaRequestHandler implements RequestHandler<String, String> {
@Override
public String handleRequest(String input, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("some log message");
return "some output";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment