oberlies
oberlies

Reputation: 11723

How to re-enable the default binding in an override

I have a TestModule which does several bindings for my unit tests, e.g. it replaces accessor classes for external systems with stubs:

bind(ExternalSystemAccessor.class).to(ExternalSystemAccessorStub.class).in(Singleton.class);

Now one of my tests needs to use the productive implementation, so I tried to bind it back the default with an override:

injector = Guice.createInjector(Modules.override(new TestModule()).with(new AbstractModule() {
    @Override
    protected void configure() {
        bind(ExternalSystemAccessor.class).to(ExternalSystemAccessor.class);
    }
}));

However, this leads to a Guice error:

com.google.inject.CreationException: Guice creation errors:

1) Binding points to itself.

So how can I get back to the default binding with an override?

Upvotes: 0

Views: 231

Answers (1)

oberlies
oberlies

Reputation: 11723

Just found the answer while typing the question. The solution is to omit the to method:

@Override
protected void configure() {
    bind(ExternalSystemAccessor.class); // re-enable default binding
}

Upvotes: 1

Related Questions