title: Android Integration test: Mocking the logic/ method in within an activity using Dagger and Mockito and get the returned value back within the test
body: <p>I am developing an Android application using Kotlin. I am also writing integrated tests for my application using Dagger and Expresso. I am now having a problem with mocking a logic. First, let me show you how I mock logic in the code using Dagger for the integrated tests.</p>
  
  <p>I have the Dagger app component class for the test with the following definition</p>
  
  <pre><code>@Singleton
  @Component(modules = [ TestAppModule::class ])
  interface TestAppComponent : AppComponent //App component is the actual class used for the application
  {
      fun inject(app: MainActivity)
  }
  </code></pre>
  
  <p>This is the definition of the TestAppModule class</p>
  
  <pre><code>@Module
  open class TestAppModule (private val app: Application) {
      @Singleton
      @Provides
      open fun fileIOService(): IFileIOService {
          return FakeFileIOService()
      }
  }
  </code></pre>
  
  <p>I also have a class called AppModule for the actual Application code with the following definition.</p>
  
  <pre><code>@Module
  open class TestAppModule (private val app: Application) {
      @Singleton
      @Provides
      open fun fileIOService(): IFileIOService {
          return FakeFileIOService()
      }
  }
  </code></pre>
  
  <p>The idea is that the integration tests will use the TestAppModule and so the FakeFileIOService class will be injected. For the actual application, it will use the AppModule and so the FileIOService class will be injected instead. Both the FakeFileIOService class and the FileIOService class will inherit from the IFileIOService interface. The IFileIOService interface will have the following definition.</p>
  
  <pre><code>interface IFileIOService
  {
      fun saveFile(data: String)
  }
  </code></pre>
  
  <p>The FileIOService class and the FakeIOService class will have their own version of the implementation for the saveFile method. The FakeIOService will have the mock version.</p>
  
  <p>I will have a property for IFileIOService in the MainActivity class for dependency injection.</p>
  
  <pre><code>class MainActivity: AppCompatActivity()
  {
      @Inject
      lateinit var fileIOService: IFileIOService
  }
  </code></pre>
  
  <p>As I mentioned, the FakeFileIOService class will be injected for the integration tests. The code works fine. It uses the fake version for the integration tests and the concrete for the actual application. </p>
  
  <p>But now, I am having a problem where I need to mock the logic of the method using Mockito and get the returned value within the tests.</p>
  
  <p>But, now I am having an issue with mocking the logic using Mokito to test File and getting the mocked value back within the tests.</p>
  
  <p>Following is the implementation of my </p>
  
  <pre><code>fun saveFile(data: String) {
      FileOutputStream mockFos = Mockito.mock(FileOutputStream.class);
      mockFos.write(data.getBytes());
      mockFos.close();
  }
  </code></pre>
  
  <p>Literally, I am trying to mock the file logic using Mockito. Then I am trying to write an integration test something like this.</p>
  
  <pre><code>@get:Rule
  var mainActivityRule: ActivityTestRule&lt;MainActivity&gt; = ActivityTestRule&lt;MainActivity&gt;(CameraActivity::class.java, true, false)
  
  @Test
  fun fileIsSaveInWhenButtonIsClicked() {
      Intents.init()
      var targetContext: Context = InstrumentationRegistry.getInstrumentation().targetContext
      var intent: Intent = Intent(targetContext, MainActivity::class.java)
      this.mainActivityRule.launchActivity(null) 
      onView(withId(R.id.button_save_file)).perform(click())
  
      //here I want to assert if the file is saved using Mockito something like this
      //verify(mockFos).write(data.getBytes()); the issue is that I cannot get the mockFos instance creatd in the saveFile method of the FakeFileIOService class
      Intents.release()
  }
  </code></pre>
  
  <p>See the comment in the test. The issue is that I want to verify if the file is saved using Mockito. But the issue is that I cannot get the mockFos instance created in the saveFile of the FakeIOService class. How can I test it? What would be the best way to test this scenario?</p>