Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access internal components from test source with Android

Tags:

android

kotlin

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?

like image 366
nhaarman Avatar asked Nov 18 '15 15:11

nhaarman


People also ask

What is Mockito Android?

Conclusion. Mockito is used to test final classes in Kotlin. Mockito is a widely used framework for mobile developers to write unit tests.

How do you write JUnit test cases for Android?

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.

Which Java framework is used to write unit tests in android?

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.

What is test folder in Android Studio?

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).


1 Answers

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.

like image 187
Jayson Minard Avatar answered Nov 13 '22 02:11

Jayson Minard