Overwrite module provided binding for testing

12 views
Skip to first unread message

unv...@gmail.com

unread,
May 26, 2021, 2:59:58 AM5/26/21
to Bootique User Group
Say in an integration test I have the following:

@BQApp(value = TEST_CLASS)
static final BQRuntime app = Bootique.app("--run")
     .autoLoadModules()
      .module(db.moduleWithTestDataSource("default"))))
      .createRuntime();  


How would I overwrite a binding provided by one of the auto loaded modules ?
i.e. one the (auto loaded) modules has:

binder.bind(Client.class).to(ClientImpl.class).inSingletonScope();

for the test I want to provide my own mock implementation:
binder.bind(Client.class).to(MockClientImpl.class).inSingletonScope();


Thanks.

Andrus Adamchik

unread,
May 26, 2021, 4:38:26 AM5/26/21
to Bootique User Group
Hi,

A more verbose option is to create a subclass of BaseModule for your test, implement "overrides()" method in it, as well as the "bind" method to declare the binding. Or an "inline" alternative:

Bootique.app(..)
.override(UpstreamModule.class)
.with(b -> b.bind(Client.class).to(MockClientImpl.class)

Andrus
> --
> You received this message because you are subscribed to the Google Groups "Bootique User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bootique-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bootique-user/e2aae625-6667-4411-8e8b-2a9943996756n%40googlegroups.com.

unv...@gmail.com

unread,
May 26, 2021, 5:25:09 AM5/26/21
to Bootique User Group
The "inline" alternative works perfectly. 

Thanks.

Reply all
Reply to author
Forward
0 new messages