To run tests, we must satisfy some dependencies. We will update our application configuration by extending build.gradle to support testing and to provide the classes we need. Open build.gradle and extend it as follows:
apply plugin: "com.android.application" apply plugin: "kotlin-android" apply plugin: "kotlin-android-extensions" repositories { maven { url "https://maven.google.com" } } android { ... sourceSets { main.java.srcDirs += [ 'src/main/kotlin', 'src/common/kotlin', 'src/debug/kotlin', 'src/release/kotlin', 'src/staging/kotlin', 'src/preproduction/kotlin', 'src/debug/java', 'src/release/java', 'src/staging/java', 'src/preproduction/java', 'src/testDebug/java', 'src/testDebug/kotlin', 'src/androidTestDebug/java', 'src/androidTestDebug/kotlin' ] } ... testOptions { unitTests.returnDefaultValues = true } } ... dependencies { ... compile "junit:junit:4.12" testCompile "junit:junit:4.12" testCompile "org.jetbrains.kotlin:kotlin-reflect:1.1.51" testCompile "org.jetbrains.kotlin:kotlin-stdlib:1.1.51" compile "org.jetbrains.kotlin:kotlin-test:1.1.51" testCompile "org.jetbrains.kotlin:kotlin-test:1.1.51" compile "org.jetbrains.kotlin:kotlin-test-junit:1.1.51" testCompile "org.jetbrains.kotlin:kotlin-test-junit:1.1.51" compile 'com.android.support:support-annotations:26.0.1' androidTestCompile 'com.android.support:support
-annotations:26.0.1' compile 'com.android.support.test:runner:0.5' androidTestCompile 'com.android.support.test:runner:0.5' compile 'com.android.support.test:rules:0.5' androidTestCompile 'com.android.support.test:rules:0.5' } It is important to highlight use of: testOptions { unitTests.returnDefaultValues = true }
This will enable us to test content providers and use all related classes in our tests. If we do not enable this, we will get this error:
Error: "Method ... not mocked"!