Injection service with dagger and robot electrician

I am trying to enter the TelephonyManager service using Dagger. I follow this pattern . I extended the Application class to create a graph and defined an ApplicationModule where the ServiceModule included and other modules will be included in the future.

Edit:

Now the system service is entered on Activity without any problems. I lost FinderApplication.inject(this) inside the Activity . It has an injection, but is still not tested with Robolectric . I added a test case at the bottom of this post:

Edit-2: ApplicationModule removed and created by BaseActivity with:

 ((FinderApplication) getApplication()).getGraph().inject(this); 

on onCreate . The error I am getting is:

Called: java.lang.NoSuchMethodException: ServiceModule. ()

But if I define an empty constructor, I get a null pointer when an input class is required.

FinderApplication.java

 public class FinderApplication extends Application { private ObjectGraph mObjectGraph; @Override public final void onCreate() { super.onCreate(); mObjectGraph = ObjectGraph.create(new ServiceModule(this)); } public final ObjectGraph getGraph() { return mObjectGraph; } } 

ServiceModule.java

 @Module(entryPoints = { SimCardActivity.class, SimService.class }) public class ServiceModule { private Context mContext; public ServiceModule(Context context) { mContext = context; } @Provides @Singleton TelephonyManager provideTelephonyManager(){ return (TelephonyManager) mContext .getSystemService(Context.TELEPHONY_SERVICE); } } 

SimCardActivityTest.java

 @RunWith(RobolectricTestRunner.class) public class SimCardActivityTest { @Before public void setUp() throws Exception { ObjectGraph.create(new TestModule()).inject(this);; } @Module( includes = ServiceModule.class, overrides = true, entryPoints = {SimCardActivityTest.class, SimCardActivity.class} ) static class TestModule{ public static TelephonyManager TELEPHONY_MANAGER = Mockito.mock(TelephonyManager.class); @Provides @Singleton TelephonyManager provideTelephonyManager(){ return TELEPHONY_MANAGER; } } } 
+6
source share
1 answer

As @AndyDennie said in his comment, I did not inject in the test class. Implementing the activity being tested instead of creating it on setUp() solved the problem.

My current test case (corresponding code) is as follows:

 @RunWith(RobolectricTestRunner.class) public class SimCardActivityTest { @Inject private SimCardActivity mActivity; private TelephonyManager mTelephonyManager; @Before public void setUp() throws Exception { ObjectGraph.create(new ServiceModule(Robolectric.application), new ServiceTestModule()).inject(this); } @Module( includes = {ServiceModule.class }, overrides = true, entryPoints = {SimCardActivity.class, SimCardActivityTest.class} ) static class ServiceTestModule { public static TelephonyManager TELEPHONY_MANAGER = Mockito.mock(TelephonyManager.class); @Provides @Singleton TelephonyManager provideTelephonyManager(){ return TELEPHONY_MANAGER; } } } 
+3
source

All Articles