Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

AssistedInject factories should call their default methods without stubbing #1345

Open
ogregoire opened this issue Jun 8, 2020 · 0 comments · May be fixed by #1760
Open

AssistedInject factories should call their default methods without stubbing #1345

ogregoire opened this issue Jun 8, 2020 · 0 comments · May be fixed by #1760

Comments

@ogregoire
Copy link

I have a factory interface, extended by factory subtypes. The factories have default methods. So I would expect that they are called instead of whatever implementation is used.

Here's my full test case. Yes, I'm using a small hierarchy because that's actually part of my actual needs. It's probably not needed to showcase the error, though.

class DefaultMethodTest {

static class Foo { String str; @Inject Foo(@Assisted String str) { this.str = str; } }
static class Bar extends Foo { @Inject Bar(@Assisted String str) { super(str); } }

interface FooFactory {
	Foo create(@Assisted String str);
	default String id() { return "Foo"; }
}
interface BarFactory {
	Bar create(@Assisted String str);
	default String id() { return "Bar"; }
}
	
@Test
void testAssistedDefault() {
	var injector = Guice.createInjector(binder -> binder.install(new FactoryModuleBuilder().build(BarFactory.class)));
	var barFactory = injector.getInstance(BarFactory.class);
	assertThat(barFactory.id()).isEqualTo("Bar");
}
}

The assertion fails because the call returns the empty string "". It must be a default or something like that, I'm not entirely sure, but I would have expected that my default method is called.

@ogregoire ogregoire changed the title AssistedInject and default methods AssistedInject factories should call their default methods without stubbing Jun 8, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
1 participant