Dagger: input field provided by pojo

Currently testing with a dagger, what I want to do is instantiate and implement various Bar implementations. How can I enter fields in the provided fields? eg:

Module:

@Module(
        injects = {
                Main.class
        },
        complete = false,
        library = true
)
public class ExampleTestModule {
    @Provides
    public Foo providesFoo() {
        return new Foo();
    }
    @Provides
    public Bar providesBar(BarImpl impl) {
        // return new BarImpl(); // null
        return impl;
    }
}

Main:

public class Main {
    @Inject
    Foo foo;
}

Foo:

public class Foo {
    @Inject
    Bar bar;
}

Bar:

public interface Bar {

}

BarImpl

public class BarImpl implements Bar {
}

TestCase:

public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }


    public void testFoo() {
        Main main = new Main();
        ObjectGraph.create(new ExampleTestModule()).inject(main);
        assertNotNull(main.foo);
    }

    public void testFooBar() {
        Main main = new Main();
        ObjectGraph.create(new ExampleTestModule()).inject(main);
        assertNotNull(main.foo.bar);
    }
}

Main.Foo is not null, but Main.Foo.Bar is null.

+4
source share
1 answer

You never type barin foo.

ObjectGraph.create(new ExampleTestModule()).inject(main);

This line will display the fields mainthat are annotated @Injectand enter them. There is no recursive behavior.


Problem fix

Release step by step:

  • complete = false library = true Module. . , - , . , :

    Error:(11, 8) error: No injectable members on BarImpl. Do you want to add an injectable constructor? required by providesBar(BarImpl) for ExampleTestModule.
    
  • BarImpl, :

    public class BarImpl implements Bar {
        @Inject
        BarImpl(){
        }
    }
    
  • :

    Error:(11, 8) error: Graph validation failed: You have these unused @Provider methods:
    1. ExampleTestModule.providesBar()
    Set library=true in your module to disable this check.
    
  • -, providesBar() . , bar foo . :

    • bar :

      ObjectGraph graph = ObjectGraph.create(new ExampleTestModule());
      graph.inject(main);
      graph.inject(main.foo);
      
    • ( ):

      public class Foo {
          Bar bar;
      
          @Inject
          Foo(Bar bar){
              this.bar = bar;
          }
      }
      
  • , providesFoo(), bar foo. , . foo @Injectable, foo, . , , bar, .

  • , @Inject foo main . ObjectGraph.get(Class<?>), main.


:

:

@Module(
        injects = Main.class
)
public class ExampleTestModule {
    @Provides
    public Bar providesBar(BarImpl impl) {
        return impl;
    }
}

Main:

public class Main {
    Foo foo;

    @Inject
    Main(Foo foo) {
        this.foo = foo;
    }
}

Foo:

public class Foo {
    Bar bar;

    @Inject
    Foo(Bar bar){
        this.bar = bar;
    }
}

:

public interface Bar {
}

BarImpl:

public class BarImpl implements Bar {
    @Inject
    BarImpl(){
    }
}

ApplicationTest:

public class ApplicationTest extends ApplicationTestCase<Application> {

    public ApplicationTest() {
        super(Application.class);
    }


    public void testFoo() {
        Main main = ObjectGraph.create(new ExampleTestModule()).get(Main.class);
        assertNotNull(main.foo);
    }

    public void testFooBar() {
        Main main = ObjectGraph.create(new ExampleTestModule()).get(Main.class);
        assertNotNull(main.foo.bar);
    }
}

:

  • library = true complete = false . .
  • . , . , private, .
  • providesXXX , , bar BarImpl. , , , , ?
+9

All Articles