I have got the following structure:
build.gradle
src
- main
- AndroidManifest.xml
- kotlin
- pkg
- MyClass.kt (internal class MyClass)
- test
- kotlin
- pkg
- MyClassTest.kt
In MyClassTest.kt
I reference the internal class MyClass
.
The build.gradle
:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
//apply plugin: 'java'
//apply plugin: 'kotlin'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
}
}
dependencies {
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
buildscript {
ext.kotlin_version = '1.0.0-beta-2423'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}
Now when I run ./gradlew test
, I get the following error:
Cannot access 'MyClass': it is 'internal' in 'pkg'
However, when I switch the plugins to use java
instead of com.android.application
, and kotlin
instead of kotlin-android
, the tests do succeed.
How can I reference internal classes from my test sources when using the Android plugins?
Conclusion. Mockito is used to test final classes in Kotlin. Mockito is a widely used framework for mobile developers to write unit tests.
You write your local unit test class as a JUnit 4 test class. To do so, create a class that contains one or more test methods, usually in module-name/src/test/ . A test method begins with the @Test annotation and contains the code to exercise and verify a single aspect of the component that you want to test.
JUnit is a “Unit Testing” framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains annotations such as @Test, @Before, @After, etc.
The location of your tests depends on the type of test you write. Android projects have default source code directories for local unit tests and instrumented tests. Local unit tests are located at module-name /src/test/java/ . These are tests that run on your machine's local Java Virtual Machine (JVM).
This issue existed as bug KT-9441 (as noted by @Andrey), and has since been fixed. There should be no current problem with latest Kotlin and its Gradle plugins.
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