简体   繁体   中英

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?

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"
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM