mercredi 4 mai 2016

How to unit test a class which has field injection (using guice)

Class under test looks like this :

public class SomeAdapter {

@Inject
HttpService httpService;

@Inject
Configuration configuration;

public SomeAdapter()
{
    GuiceInjector.getInjector().injectMembers(this);
}

public String getBaseUrl()
{
    return configuration.getProtocol()+ "://" + some.getServer() + ":" + configuration.getPort();
  }
}

I have tried InjectMocks from the mockito framework, but it does not seem reliable.Would creating a seperate test module which extends AbstractModule be necessary?

Aucun commentaire:

Enregistrer un commentaire