简体   繁体   中英

How to re-enable the default binding in an override

I have a TestModule which does several bindings for my unit tests, eg 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?

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
}

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