Code Monkey home page Code Monkey logo

Comments (10)

mataiaslev avatar mataiaslev commented on May 14, 2024 4

Hi,

For insert mock objects under Espresso I am essentially configuring the tests for use another "Application" class for start. That use another startKoin(..) method, that calls our TestModules in place of the modules for the Real app. (This imply make some methods open, and probably will be improved.)

Configurations and Code:

Gradle:

defaultConfig {
        testInstrumentationRunner 'com.your.path.CustomInstrumentationTestRunner'
        ...
}

and I add the library androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito:2.12.1'
for let mockito do his job in this context.

Custom AndroidJUnitRunner under the folder of the espresso tests:

class CustomInstrumentationTestRunner: AndroidJUnitRunner() {

    @Throws(InstantiationException::class, IllegalAccessException::class, ClassNotFoundException::class)
    override fun newApplication(cl: ClassLoader,
                                className: String,
                                context: Context): Application {
        return Instrumentation.newApplication(MyIntegrationTestApp::class.java, context)
    }

}

Our application for tests:

class MyIntegrationTestApp: MainApplication() {
    override fun onCreate() {
        super.onCreate()
        val testModule = TestModule()
        val authenticator = Mockito.mock(Authenticator::class.java)
        Mockito.`when`(authenticator.isAuthenticated()).thenReturn(true)
        testModule.authenticator = authenticator
        startKoin(this, listOf(testModule))
    }
}

My TestModule looks like:

class TestModule: AndroidModule() {
    lateinit var authenticator: Authenticator

    override fun context() = applicationContext {
        context(name = "MainActivity") {
            provide { context }
            provide { authenticator }
            provide { AuthUIWrapper() }
            provide { ActionIntentGenerator(get(), get()) }
            provide { AuthViewModel(get(), get()) }
            provide { FirebaseAuth.getInstance() }
            provide { Screen(get()) }
            provide { PetFactory(get()) }
            provide { MutableLiveData<List<Pet>>() }
            provide { PetHelper() }
            provide { PetAdapter() }
            provide { FirebaseFirestore.getInstance() }
            provide { PetViewModel(get(), get(), get()) }
            provide { FirestorePetRepository(get(), get()) } bind PetRepository::class
        }

    }
}

So, I can set the Authenticator with the mock I want for the Espresso tests.

# RELATED TO THE UNIT TESTS:

What I do is create a TestModule: (Extends for Module and not for AndroidModule in this context) and startKoin into the tests with this modules.

class MyTestModule: Module() {

    lateinit var petRepository: PetRepository
    lateinit var petFactory: PetFactory
    lateinit var petHelper: PetHelper

    override fun context(): Context = applicationContext {
        provide { petRepository }
        provide { petFactory }
        provide { petHelper }
    }
}

Test example: we are using startKoin on koin context, just need the list of modules.

"PetViewModel should fetch data from the pet repository, when solicited data" {
            val testData: LiveData<List<Pet>> = MutableLiveData()
            val repository = Mockito.mock(PetRepository::class.java)
            val petFactory = Mockito.mock(PetFactory::class.java)

            val myTestModule = MyTestModule()
            myTestModule.petRepository = repository
            myTestModule.petFactory = petFactory

            startKoin(listOf(myTestModule))

            val petViewModel = PetViewModel()
            `when`(repository.getAllPets()).thenReturn(testData)

            val data = petViewModel.getAllPets()

            verify(repository).getAllPets()
            data shouldBe testData
        }

I am using kotlintest. And I can insert mocks :)

Thanks for all, And tell me please if something is not clear. I have experience in development but I am very new sharing my knowledge hahaha so Just tell me and I can improve :)

from koin.

arnaudgiuliani avatar arnaudgiuliani commented on May 14, 2024 2

Hello,

this is one the hot topics for Koin. Anybody with Espresso knowledge is welcome to give help for Koin 👍

The article you mention deals about unit testing (testing before the UI) - Which is (in my point of view) essential to do before making any UI test.

I'll put that on top priority.

from koin.

arnaudgiuliani avatar arnaudgiuliani commented on May 14, 2024 1

Instrumented test are compatible as is with Koin. I will document it

from koin.

dGorod avatar dGorod commented on May 14, 2024

Test project where want to try it first: https://github.com/dGorod/test_500px_api_kotlin

from koin.

arnaudgiuliani avatar arnaudgiuliani commented on May 14, 2024

Interesting 👍

from koin.

cyberratt avatar cyberratt commented on May 14, 2024

You could just change your myTestModule, for instance instead of assigning a mock to the petRepository, just changes the provide method to a 'mock(PetRepository::class.java) as PetRepository' or if you're using mockito-kotlin just mock<PetRepository>()

from koin.

arnaudgiuliani avatar arnaudgiuliani commented on May 14, 2024

In fact, Koin is already compatible with Android instrumented espresso tests. Just need precise explanation on what and how to do things

from koin.

hoombar avatar hoombar commented on May 14, 2024

I couldn't find any documentation around Espresso testing or any samples - are these in the works?

from koin.

arnaudgiuliani avatar arnaudgiuliani commented on May 14, 2024

You can find espresso/room tests in https://github.com/InsertKoinIO/koin/tree/1.0.0/koin-projects/examples/android-mvvm/src/androidTest/java/fr/ekito/myweatherapp

Just use the KoinTest interface to tag your test class and use Koin features. The espresso test will use the default Koin configuration from your application. You can then override definitions to help you make your tests.

A good article https://proandroiddev.com/testing-with-koin-ade8a46eb4d

from koin.

cynw avatar cynw commented on May 14, 2024

In addition to @mataiaslev answer, my project is using MockMaker mockito extension to run unit test.
I got this compile error

More than one file was found with OS independent path 'mockito-extensions/org.mockito.plugins.MockMaker'

To fix this error, add exclude group: 'org.mockito' to the dependency in the gradle file.

androidTestImplementation("org.koin:koin-test:1.0.2") { exclude group: 'org.mockito' }

from koin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.