PLahm21
PLahm21

Reputation: 97

How to unit test aws lambda invokation?

i have a aws lambda RequestHandler written in Java which invokes another aws lambda. To invoke the other lambda i wrote an Interface with methods annotaded by @LambdaFunction. To build an invoker I use:

LamdaInvoker lamdaInvoker = LambdaInvokerFactory.builder().lambdaClient(AWSLambdaClientBuilder.defaultClient()).build(LamdaInvoker.class);

When I run the JUnit tests without having deployed the lambda I get:

java.lang.IllegalArgumentException: No region provided

Is it possible to Mock the Invoker in the unit tests and how?

Upvotes: 4

Views: 5464

Answers (1)

Naman
Naman

Reputation: 32038

You can try something on the lines of this within your Test class -

private LambdaInvocationHandler handler;
private AWSLambda lambda;
private LambdaInvokerFactory factory;
private YourInterface invoker;

@Before
public void setup() {
    lambda = Mockito.mock(AWSLambda.class);
    factory = new LambdaInvokerFactory(lambda, null);
    invoker = factory.build(YourInterface.class);
    handler = (LambdaInvocationHandler) Proxy.getInvocationHandler(invoker);
}

where your interface assumingly is -

static interface YourInterface {
    @LambdaFunction
    void someMethod(String arg1);
}

and your test method could be -

@Test
public void testSomeMethod() throws Exception {
    Method x = getMethod("someMethod", String.class);
    handler.validateInterfaceMethod(someMethod, new Object[] {
            "str"
    });
}

Upvotes: 1

Related Questions