Mocking a Supplier<>-Bean
I would like to mock a Bean (using mockito) that is defined like so
@Bean("idGenerator")
public Supplier<UUID> idGenerator() {
return () -> UUID.randomUUID();
}
In a SpringBootTest
-class I get an error using @MockBean
indicating, that that Bean cannot be mocked (due to some limitations in the JVM? - sorry, I don't have the stacktrace at hand right now).
I came up with a workaround that does not use Mocks but an additional field in a @TestConfiguration
so that the return-value of the supplier can be specified externally.
Since I don't rea开发者_运维百科lly like that workaround (and my colleagues won't either), I hope there is a proved pattern or the realization I am doing that mocking wrong.
You can just define it like follows:
@MockBean(name = "idGenerator")
private Supplier<UUID> mockedSupplier;
there is no issue that prevents this from mocking. Would be good to include your stacktrace, as the issue is probably somewhere else.
精彩评论