5

Google recently released experimental support for running junit on local JVM in Android Studio. I wanted to try it out and since I am developing my Android app using Kotlin I want my tests classes be written in Kotlin too.

Unfortunately, I can't make it working. I have updated my gradle configuration:

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
    androidTest.java.srcDirs += 'src/androidTest/kotlin'
    test.java.srcDirs += 'src/test/kotlin'//this line was added
}
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"

Android Studio recognise test folder.

This is my simple test class just to see if it works

public class MyFirstTestClass {

    [Test] fun firstTest() {
        println("I am OK")
        Assert.assertTrue("".isEmpty())
    }
}

If I try to run it I get the following message

Class not found: 'com.example.MyFirstTestClass'

1 Answer 1

4

Unfortunatly, kotlin-gradle plugin doesn't support Junit tests for Android at this moment.

EDIT: Junit support for android was added in Kotlin M12

Sign up to request clarification or add additional context in comments.

3 Comments

For completeness, you can still use Java based unit tests to test your Kotlin classes.
is there any way to auto-generate a java junit test for a kotlin class? Go To -> Test is greyed out for me unfortunately for .kt classes
In Kotlin Beta 1 RC announcement it was mentioned that "The last but not least, unit testing experience is much smoother now. List of improvements: “Create Test” action, tests are runnable via gutter icons, navigation between tests and test subjects (⇧⌘T/⇧^T) and also quickfixes to add dependencies on JUnit and TestNG when needed" ... which gives you those features you are looking for with navigating unit tests. @ZakTaccardi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.