I have a module in my class called MainModule. It has various bindings in it, one of which is a binding to my custom service interface called HttpClientPool
public class MainModule extends AbstractVertxModule{
binder.bind(HttpClientPool.class).to(HttpClientPoolImpl.class).in(Singleton.class);
// other bindings
}
@Provides @Singleton
public TokenClient tokenClient(HttpClientPool clientPool){
return new TokenClient(clientPool);
}
The module is also a provider of an object called tokenClient, as seen above.
The tokenClient is being injected somewhere else in a different class and a few methods are being invoked on this object.
In my unit tests, I'm intending to use Mockito to get a mocked tokenClient object. That means, I would like MainModule to provide a mocked object, instead of a real one. I have tried using a testMainModule that looks like this:
public class testMainModile implements Module{
private TokenClient tokenClient;
public TokenModule(TokenClient client) {
this.tokenClient= client;
}
@Override
public void configure(Binder binder) {
binder.bind(TokenClient.class).toInstance(tokenClient);
}
}
An excerpt from my unit tests:
@Mock
private TokenClient tokenClient;
// Stuff between
Injector messagingInjector = Guice.createInjector(new TestMainModule(tokenClient));
mainModule = messagingInjector.getInstance(MainModule.class);
Somehow, all I get is a real object from the mainModule object. Am I missing something?
injector.getInstance(TokenClient.class)
on an Injector you create withnew MainModule()
(production) ornew TestMainModule()
(tests). Arguably, your unit tests shouldn't use Guice injectors at all, but that advice still holds true for system or integration tests. – DesperadoModule
from anInjector
. – KuhltokenClient
from thenew TestMainModule();
. How do I pass it on to aMainModule
object being used in my tests? – Jacintojack