3
我想把我的測試從java轉換成kotlin。Kotlin androidTest:測試運行完成。空測試套件
簡單的單元測試成功轉換,就像這樣:
class BindingUtilsTest {
@Test @Throws(Exception::class)
fun testConvertBooleanToVisibility_visible() {
assertEquals(BindingUtils.convertBooleanToVisibility(true), View.VISIBLE)
}
}
但是,當我試圖運行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}"
簡單的測試:
@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)
}
}
測試組件是在科特林:
// Empty because extends ApplicationComponent
@Singleton @Component(modules = arrayOf(
AppModuleTest::class)) interface AppComponentTest : AppComponent
和測試模塊也是科特林:
@Module class AppModuleTest(private val mApp: Questions) /*: AppModule*/ {
@Provides fun provideApp(): Questions {
return mApp
}
}
我甚至沒有看到,DaggerAppComponentTest是建立的。
爲什麼我使用apt而不是kapt進行測試?
因爲我有一個錯誤,我不能在一個項目中混合apt和kapt。我試圖切換到kapt,並有數十個錯誤。
據我所知,kapt處理kotlin文件並使用它來生成kotlin代碼?對於apt:java文件,java代碼。如何混合?如何解決這個問題呢?
解決方案
接受的解決方案的工作。在此之前,我爲Kotlin返回了kapt。用kaptAndroidTest
和kaptTest
。
是的,這項工作給我。但是對於依賴項中的Realm,我必須在測試之前每次清理項目,以使其正常工作。太多的問題:( –