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,我必须在测试之前每次清理项目,以使其正常工作。太多的问题:( –