Guice 4.0beta will only return a list of Element elements. Therefore, I modify the Jesse Wilson code as follows. You need to provide a list of modules and subtract the binding to the target you want to replace.
Injector injector = Guice.createInjector(new TestGuiceInjectionModule(), Utils.subtractBinding(new GuiceInjectionModule(),Key.get(IInfoQuery.class)));
Function
public static Module subtractBinding(Module module, Key<?> toSubtract) { List<Element> elements = Elements.getElements(module); return Elements.getModule(Collections2.filter(elements, input -> { if(input instanceof Binding) { return !((Binding) input).getKey().equals(toSubtract); } return true; })); }
source share