Ant*_*nko 5 java android kotlin android-testing dagger-2
我正在尝试将测试从Java转换为Kotlin。
简单的单元测试已成功翻译,如下所示:
class BindingUtilsTest {
@Test @Throws(Exception::class)
fun testConvertBooleanToVisibility_visible() {
assertEquals(BindingUtils.convertBooleanToVisibility(true), View.VISIBLE)
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行androidTest时,它失败并显示以下消息:“未找到测试”,并且
测试运行已开始
测试已完成。空的测试套件。
在使用Java时,代码可以完美运行。相关代码:
build.gradle部分:
apply plugin: "com.android.application"
apply plugin: "com.neenbedankt.android-apt"
// for tests
apply plugin: 'kotlin-android'
// defaultConfig
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
sourceSets {
test.java.srcDirs += 'src/test/kotlin' // tests are there
androidTest.java.srcDirs += 'src/androidTest/kotlin' // and there
}
// unit tests
testApt "com.google.dagger:dagger-compiler:${daggerVer}"
// kotlin
testCompile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVer}"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:${kotlinVer}"
// android tests
androidTestApt "com.google.dagger:dagger-compiler:${daggerVer}"
// kotlin
androidTestCompile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVer}"
androidTestCompile "org.jetbrains.kotlin:kotlin-test-junit:${kotlinVer}"
Run Code Online (Sandbox Code Playgroud)
简单测试:
@RunWith(AndroidJUnit4::class) class MainDrawerActivityTest {
private val mQuestions = InstrumentationRegistry.getTargetContext().applicationContext as Questions
private val mTestComponentRule = TestComponentRule<Questions, AppComponentTest>(mQuestions,
DaggerAppComponentTest.builder().appModuleTest(AppModuleTest(mQuestions)).build(),
{ obj, component -> obj.setAppComponent(component) }, // set test component
{ objectToClear -> objectToClear.setAppComponent(null) }) // clear test component
private val mActivityTestRule = ActivityTestRule(
MainDrawerActivity::class.java, false, false)
// TestComponentRule needs to go first to make sure the Dagger TestComponent is set
// in the Application before any Activity is launched.
@Rule @JvmField val mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
private var mActivity: MainDrawerActivity? = null
@Before @Throws(Exception::class)
fun setUp() {
mActivityTestRule.launchActivity(null)
mActivity = mActivityTestRule.activity
}
@Test @Throws(Exception::class)
fun testOnCreate() {
val size = mActivity!!.supportFragmentManager.fragments.size
// check if fragment instantly added
assertEquals(size.toLong(), 1)
}
}
Run Code Online (Sandbox Code Playgroud)
测试组件在Kotlin中:
// Empty because extends ApplicationComponent
@Singleton @Component(modules = arrayOf(
AppModuleTest::class)) interface AppComponentTest : AppComponent
Run Code Online (Sandbox Code Playgroud)
测试模块也位于Kotlin中:
@Module class AppModuleTest(private val mApp: Questions) /*: AppModule*/ {
@Provides fun provideApp(): Questions {
return mApp
}
}
Run Code Online (Sandbox Code Playgroud)
我什至看不到DaggerAppComponentTest是内置的。
为什么我使用apt而不是kapt进行测试?
因为出现一个错误,我无法在一个项目中混用apt和kapt。我试图改用kapt并得到数十亿个错误。
据我了解,kapt处理kotlin文件,人们使用它会生成kotlin代码吗?对于apt:java文件,java代码。如何混合?如何解决这个问题呢?
解
公认的解决方案有效。在此之前,我将kapt送回Kotlin。与kaptAndroidTest和kaptTest。
改变
@Rule @JvmField val mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
Run Code Online (Sandbox Code Playgroud)
到
@get:Rule @JvmField var mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
Run Code Online (Sandbox Code Playgroud)
如果不起作用,则说明 mRuleChain 为空,请检查 Dagger 提供的对象。
| 归档时间: |
|
| 查看次数: |
1657 次 |
| 最近记录: |