google / google/guice

AssistedInject factories should call their default methods without stubbing

Open
#1,345 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.