我一直在尝试使用Espresso
作为DI工具进行简单的Koin
单元测试。这是我在build.gradle
// testing with Koin
// because of this
// https://github.com/InsertKoinIO/koin/pull/604/commits/69391bc378bbb9007b9d82c46537e7d753be7ea3
androidTestImplementation 'org.mockito:mockito-android:3.1.0'
androidTestImplementation ("org.koin:koin-test:$koin_version") {
exclude group: 'org.mockito'
}
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// stuff like activityTestRule
androidTestImplementation 'androidx.test:rules:1.2.0'
// AndroidJUnit4
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
// test runner
androidTestImplementation 'androidx.test:runner:1.2.0'
我的ViewModel
声明
open class LoginViewModel(private val apiService: MockApiService) : ViewModel() {
..
..
}
这是它在“活动”中的注入方式
private val loginViewModel: LoginViewModel by viewModel()
我的自定义TestRunner
以便实例化自定义TestApplication
class MyTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader?,classname: String?,context: Context?): Application {
return super.newApplication(cl,TestApplication::class.java.name,context)
}
}
TestApplication
类。我已经验证了调用测试时该测试类是否已初始化
class TestApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidLogger()
androidContext(this@TestApplication)
modules(emptyList())
}
}
}
这是我的实际androidTest
。通过NoBeanDefFoundException
未找到“ com.abhishek.mvvmdemo.onboarding.LoginViewModel”的定义。
@RunWith(AndroidJUnit4::class)
@LargeTest
class LoginactivityTest : KoinTest {
private lateinit var loginViewModel: LoginViewModel
@get:Rule
val activityRule = activityTestRule(Loginactivity::class.java)
@Before
fun beforeTest() {
loginViewModel = declareMock()
loadKoinmodules(
module {
// single { ApiModule.providesApiService() }
viewModel { loginViewModel }
}
)
}
@Test
fun testProgress() {
activityRule.launchactivity(null)
onView(withId(R.id.emailEt))
.perform(Viewactions.typeText("abhishek"))
}
@After
fun afterTest() {
stopKoin()
}
}
我尝试了很多排列和组合,但是没有运气。我的gradle中也碰巧有以下配置
testOptions {
animationsDisabled = true
}
packagingOptions {
pickFirst 'mockito-extensions/org.mockito.plugins.MockMaker'
}
和
testInstrumentationRunner "com.abhishek.mvvmdemo.MyTestRunner"
TL; DR
这里是github sample,再现了问题