jeudi 20 juillet 2017

Android testing with MVP + Dagger 2

I am creating Android application using pattern MVP with Dependency Injection Framework Dagger 2. Application has custom IntentService for sending requests to REST. Every Activity has own Dagger component for providing all required objects including Presenter(Presentation layer) and Interactor(Model layer) classes. Presenter contains code without Android SDK - only logic. Interactor can launch IntentService to receive data, contains ApplicationContext, etc.

For testing my Java/Kotlin classes I am using JUnit and my own mock classes. For Instrumented testing with Android SDK I am using Espresso.

Here is simple test how I am doing it(Kotlin):

@RunWith(AndroidJUnit4::class)
@MediumTest
class SampleBehaviorTest : UiBehaviorTestBase() {

    @get:Rule
    val rule = IntentsTestRule<SampleActivity>(SampleActivity::class.java, true, false)

    @Inject lateinit var someService: SomeService

    @Before
    fun setUp() {
        (getApp().buildSampleComponent() as MockSimpleComponent).inject(this)
        val intent = Intent()
        rule.launchActivity(intent)
    }

    @Test
    fun should_display_trade_on_receive() {
        someService.doSomeJob()
        // Assert equals or smth else
    }
}

Problem: It is really problematic to test classes with DI - when I launch tests first time - it passes, second - one test fails, third - another test fails and first one passed, and so on.

Question: What is correct way to test Activity with IntentService, DI and MVP pattern? Is it possible to do that in unit testing?

Aucun commentaire:

Enregistrer un commentaire