I'm trying to translate my tests from java to kotlin.
Simple unit tests were translated successfully, like this:
class BindingUtilsTest {
@Test @Throws(Exception::class)
fun testConvertBooleanToVisibility_visible() {
assertEquals(BindingUtils.convertBooleanToVisibility(true), View.VISIBLE)
}
}
But when I'm trying to run androidTest it fails with message: "No tests were found" and
Test running started
Tests ran to completion.Empty test suite.
Code worked perfectly, when was in java. Related code:
build.gradle parts:
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}"
Simple test:
@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)
}
}
Test component is in Kotlin:
// Empty because extends ApplicationComponent
@Singleton @Component(modules = arrayOf(
AppModuleTest::class)) interface AppComponentTest : AppComponent
And test module is also in Kotlin:
@Module class AppModuleTest(private val mApp: Questions) /*: AppModule*/ {
@Provides fun provideApp(): Questions {
return mApp
}
}
I don't even see, that DaggerAppComponentTest is built.
Why I use apt instead of kapt for tests?
Because I've got an error that I can't mix apt and kapt in one project. I tried to switch apt to kapt and got billion of errors.
As I understand, kapt processes kotlin files and using it people generate kotlin code? And for apt: java files, java code. How to mix it? How to solve this problem?
Solution
Accepted solution works. Before that I returned kapt for Kotlin. With kaptAndroidTest
and kaptTest
.
Change
@Rule @JvmField val mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
to
@get:Rule @JvmField var mRuleChain: TestRule = RuleChain.outerRule(mTestComponentRule).around(mActivityTestRule)
if it won't work, it means that mRuleChain is null, check Dagger provided objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With