micronaut-test
micronaut-test copied to clipboard
Support `@SpyBean`
Feature description
I was looking for having in the same class some mocking tests and non-mocking tests.
It would be very handy if we can support something similar to @SpyBean
in Spring.
I tried to use with @MockBean
without a success (got a StackOverflowError
). Maybe I am doing something wrong, I am not sure.
@MicronautTest
class MyTest {
@Inject MyService myService;
@MockBean(MyService.class)
public MyService spyMyService() {
return spy(myService);
}
// ...
}
Right now my solution is having two separated test classes (one mocking the bean with @MockBean and another one not mocking it).
If there is a better workaround I would appreciate it!
This indeed is what I was looking for. Can you share your workaround? I use kotest w/ kotlin but I think it'd be similar. I use coroutines and BlockingHttpClient.retrieve so I don't have stack traces but I guessing from a delay it might be StackOverFlow.
Hi @jacekgajek my "workaround" was having two separated test classes.
- In one test class, I use
@MockBean
- Another test class I don't mock that specific bean and let it be the real one
This might be a suitable workaround until something else is implemented.
solution in kotlin as this is something we have in our own codebase and working with the kotlin mockk library.
@MicronautTest
class MyTest {
@Inject
lateinit var myService: MyService;
@Singleton
fun spybean(): BeanCreatedEventListener<MyService> {
return BeanCreatedEventListener<MyService> { spyk(it.bean) }
}
// ...
}
Edit: to get this to work from within the same test, then the injected value has to be wrapped in a BeanProvider