Comments (10)
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.
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.
Instrumented test are compatible as is
with Koin. I will document it
from koin.
Test project where want to try it first: https://github.com/dGorod/test_500px_api_kotlin
from koin.
Interesting 👍
from koin.
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.
In fact, Koin is already compatible with Android instrumented espresso tests. Just need precise explanation on what and how to do things
from koin.
I couldn't find any documentation around Espresso testing or any samples - are these in the works?
from koin.
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.
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)
- Koin 3.5 is not binary-compatible with 3.4 HOT 2
- [koin-android] broken shared view model factory when migrating from 3.5.0 -> 3.5.3 HOT 1
- Compose view model constantly returning new instances with non-root scope HOT 5
- No static method makeOptions$default(Lorg/koin/core/module/Module;ZZILjava/lang/Object;)Lorg/koin/core/definition/Options; in
- KoinAppAlreadyStartedException after changing theme HOT 5
- > Task :composeApp:compileKotlinIosSimulatorArm64 FAILED error: Could not find "org.jetbrains.compose.annotation-internal:annotation" HOT 2
- [koin-android-compose] Broken view model factory when migrating from 3.5.0 -> 3.5.3/3.5.4 HOT 6
- Upgrading Koin `3.5.3` -> `3.5.4` removes dep on `kotlin-test` HOT 1
- No sources published for various modules of Koin 3.5.4 HOT 2
- Support default value in constructor DSL
- Docs what does int mean createdAtStart, to be created at start (or when you want)
- Kotlin Multiplatform ViewModel support in Koin HOT 3
- Missing Source Code HOT 1
- Add Object support for koin annotation.
- Does koin support component lifecycle management?
- Question - activityScope and activityRetainedScope
- ConcurrentModificationException iOS crash on koin 3.5.6
- KoinAppAlreadyStartedException after closing the app and relaunching it quickly
- Support circuit for code gen
- SLF4JLogger is using the wrong level when printing to logger
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from koin.