Skip to content

MicronautLambdaTest and MockBean don't work as expected #1870

@chenlin2001

Description

@chenlin2001

Expected Behavior

(Raised it also in mictonaut-core, but thought it should probably be here - micronaut-projects/micronaut-core#9708)

When having a MicronautLambdaTest and a MockBean I expect that the MockBean method will be called once on the https://github.com/beforeeach and that mock will be available in my tests, so I could use Mockito's verify methods.
This is the behaviour when using MicronautTest

Actual Behaviour

While the actual bean is mocked and the regular bean is not being called during the test, it seems that the been is been mocked multiple times, so I don't have one consistent mock I can do verify methods on

Steps To Reproduce

Lambda:

package com.mimecast.micronaut.lambda;

import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;
import com.mimecast.micronaut.lambda.service.HelloWorldService;
import io.micronaut.context.ApplicationContext;
import io.micronaut.function.aws.MicronautRequestHandler;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FunctionRequestHandler extends MicronautRequestHandler<ScheduledEvent, Void> {


    @Inject
    private HelloWorldService helloWorldService;

    //used in AWS
    public FunctionRequestHandler() {
    }

    //used in tests
    public FunctionRequestHandler(ApplicationContext applicationContext) {
        super(applicationContext);
    }

    @Override
    public Void execute(ScheduledEvent input) {
        log.info("input: {}", input);
        helloWorldService.handleInvocation(input.getAccount(), input.getTime());
        return null;
    }
}

Test Class

package com.mimecast.micronaut.lambda;
import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;
import com.mimecast.micronaut.lambda.service.HelloWorldService;
import com.mimecast.micronaut.lambda.service.HelloWorldServiceImpl;
import io.micronaut.context.ApplicationContext;
import io.micronaut.function.aws.test.annotation.MicronautLambdaTest;
import io.micronaut.test.annotation.MockBean;
import jakarta.inject.Inject;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;


@MicronautLambdaTest
class FunctionRequestHandlerTest {
    @Inject
    private ApplicationContext context;

    @Inject
    private HelloWorldService helloWorldService;

    @Test
    void testHandler(){
        FunctionRequestHandler functionRequestHandler = new FunctionRequestHandler(context);
        ScheduledEvent scheduledEvent = new ScheduledEvent();
        String account = "AccountA";
        DateTime dateTime = DateTime.now();
        scheduledEvent.setAccount(account);
        scheduledEvent.setTime(dateTime);
        functionRequestHandler.execute(scheduledEvent);
        verify(helloWorldService).handleInvocation(account, dateTime);
    }

    @MockBean(HelloWorldServiceImpl.class)
    HelloWorldService helloWorldService() {
        return mock(HelloWorldService.class);
    }
}

This fails on:
Wanted but not invoked:
helloWorldService.handleInvocation(
"AccountA",
2023-08-14T08:22:29.100+01:00
);

When debugging you can see that the helloWorldService method in the test is invoked multiple times. Doing something similar in Micronaut Application with @MicronautTest will only trigger the helloWorldService method once, and the test succeeds

Environment Information

MacOS Ventura 13.5
Micronaut version 4.0.3
Open JDK 17

Example Application

No response

Version

4.0.3

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions