I have a simple Micronaut Lambda Application and am having difficulty in successfully getting Mocks injected in to my unit tests.
build.gradle
micronaut {
runtime("lambda")
testRuntime("junit5")
...
}
With the following basic Controller
@Controller
public class MyController{
Service service;
@Inject
public Controller controller(Service service){
this.service = service;
}
@Post("/test")
public void someEndpoint(){
service.doSomething();
}
}
And the following test class:
@MicronautLambdaTest
public class MyControllerTest{
@Inject
Service service;
@Inject
@Client("/")
RxHttpClient rxHttpClient;
@MockBean(Service.class)
Service service() {
return mock(Service.class);
}
@Test
void should_do_something_when_something(){
rxHttpClient.toBlocking().exchange(HttpRequest.POST("/test",""));
verify(service, times(1)).doSomething();
}
}
The test above will fail as the mock is never interacted with.
When debugging I can see that the Service is mocked in the test class itself, but in the MyController class there is an actual object being used.
I notice that when I am not using runtime("lambda") (e.g. runtime("netty")) this works as expected, so I think this is something to do with the Lambda runtime, and am hoping someone may be able to point me in the right direction here.