简体   繁体   中英

Does Mockito support @Any @Inject with CDI's Instance<> interface?

I have a class containing the following code:

@Inject
@Any
private Instance<SomeInterface> SomeInterfaces;

This allows me to then iterate over all implementations of that interface. Now, I want to test this, so I declare some @Mock 's of those interfaces, and want to @InjectMocks them into my unit under test. But that fails.

Is it possible to do something like that nicely, or do I have to create a @Mock for Instance<SomeInterface>

Mockito can do the full job for unit tests. Here you want to test how you code integrates with the CDI context => you need an instance of the CDI context that you use. Of course, you could ask Mockito to inject some mocks, but that way you will test Mockito internal injection system and not your CDI context. And Mockito warns you:

Mockito is not an dependency injection framework, don't expect this shorthand utility to inject a complex graph of objects be it mocks/spies or real objects.

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