How to test the layout of interaction with Activity onResume () using dagger modules and robotoelectrics?

I use Dagger for my dependency injection, it works well in my application, but I have problems testing it. I followed this pattern to create a dependency graph for the modules: https://github.com/pyricau/shipfaster/blob/master/src/main/java/com/squareup/shipfaster/common/ShipFasterApplication.java

Now, in my test class MainActivity, I want to be able to test the interaction with the layout when the Activity onResume () method is called.

Here is the class:

@Config(emulateSdk = 18) @RunWith(RobolectricDaggerTestRunner.class) public class MainActivityTest extends TestCase { @Inject MainActivity sut; public @Mock MyObject mockMyObject; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); ObjectGraph.create(new TestModule()).inject(this); } @Test public void testThatMyActivityDelegatesDoSomethingToMyObject(){ //init ActivityController<MainActivity> activityController = ActivityController.of(sut); //run activityController.create().start().resume(); //verify Mockito.verify(mockMyObject).doSomething(); } @Module( includes = {ActivityModule.class}, injects = MainActivityTest.class, overrides = true, library = true ) class TestModule { @Provides MyObject provideMyObject() { return mockMyObject; } } } 

From what I see, the onCreate() method is onCreate() , but a real instance of myObject , not a fake one. The test failed with "desired, but not called." Actually there was zero interaction with this layout. "Error.

Perhaps this is due to the fact that the MainActivity that I am trying to create using Robolectric is not related to my TestModule because it was created at the application level, but I manage to make this test pass by explicitly calling the method in MainActivity and placing myObject.doSomething (), but I need to test the calls on the Android life cycle.

Any idea on how I can verify this?

+5
source share
2 answers

The real object is used because I assume that you have the ObjectGraph initialization in your Application class. When you call ((Application) getApplication()).inject(this) during the tests, you use the same ObjectGraph as when starting your application.

In this test, you create a completely new ObjectGraph with a mock instance of MyObject . This layout is only entered in MainActivityTest because when it is MainActivity in MainActivity inject() , it uses the ObjectGraph made in Application .

What you can do is make the TestApplication class (it must have the same package as your Application class, but must be in the test directory) whitch extends your application and adds your TestModule to override the real instances with mocks. For example, as follows:

Myapplication.java

 package com.example.myapp; public class MyApplication extends Application { ObjectGraph graph; private Account currentAccount; @Override public void onCreate() { super.onCreate(); graph = ObjectGraph.create(getModules().toArray()); init(); } void init() { // initialization stuff should not be called in tests } List<Object> getModules() { List<Object> modules = new ArrayList<>(); modules.add(new ActivityModule(this)); return modules; } public void inject(Object object) { graph.inject(object); } } 

TestMyApplication.java

 package com.example.myapp; public class TestMyApplication extends MyApplication { @Override void init() { } @Override List<Object> getModules() { modules = super.getModules(); modules.add(new TestModule()); return modules; } } 
+1
source

I had the same problems a while ago, but I managed to solve it as follows: Android testing with Robolectric and a dagger

The WojciechKo suggestion may work in some cases, but my solution may work without overriding the Application class in your tests. The difference is that you still need to provide a way to inject Dagger modules into your application, rather than creating them in the Application class. Thus, in your test class, you can add a TestModule to override the one used in the real Application class.

If you have problems with the solution presented in this link, let me know and we can continue to research the problem.

0
source

Source: https://habr.com/ru/post/1209107/


All Articles