I want inject an inteface and dagger automatically inject my implemntation but i got error
public interface HouseInterface {
String sayHello();}
*the implementation of interface is below *
public class Stark implements HouseInterface{
@Inject
public Stark(){}
@Override
public String sayHello() {
return "stark";
}
}
the house interface mosule
@Module public class HouseModule {
@Provides
public HouseInterface getHouse(Stark stark){
return stark;
}
}
*house component : *
@Component(modules = HouseModule.class) interface HouseComponent {
HouseInterface getHouse();
}
War.class :
public class War {
@Inject
public War() {
}
//#1
//this not working
@Inject
HouseInterface houseInterface;
//this works
//#2
// @Inject
//Stark stark;
//#3
// this works//HouseComponent houseComponent = DaggerHouseComponent.builder().houseModule(new HouseModule()).build();
//HouseInterface houseInterface = houseComponent.getHouse();
public String getHouseName() {
return houseInterface.sayHello();
}
}
WarComponent class
@Component
public interface WarComponent {
War getWar();
void inject(War war);
}
when i test code with below codes :
@Test
public void printNameTest() {
String expected = "stark";
WarComponent component = DaggerWarComponent.create();
War war= component.getWar();
Assert.assertEquals(war.getHouseName(), expected);
}
if in War class we use #1 to inject, not working, but if use #2, or #3 that in above codes are commented injection doesnt work , any one has any idea ? because i dont want that war.class be dependent on Stark.class and i want that it just depends on HpuseInterface so i need the #1 working