Many Android developers often face an annoying issue: Kotlin JUnit tests not recognized in Android Studio. This happens when your test classes or methods do not appear in the test runner, or the IDE fails to detect them as valid JUnit tests. In this article, we will dive deep into the causes of this problem, how to fix it step by step, and provide complete code examples to help you run tests seamlessly in your Kotlin Android project.

1. Understanding the Problem
JUnit is the most commonly used testing framework in Android development. However, when working with Kotlin in Android Studio, you may notice that your test methods are grayed out or that the IDE shows “No tests were found”. This usually happens because:
- JUnit dependencies are missing or mismatched in
build.gradle. - Incorrect test annotations are used.
- The test class is not placed in the correct test directory (
src/testorsrc/androidTest). - Android Studio cache or Gradle sync issues.
2. Setting Up JUnit in Kotlin
Before running tests, ensure that your build.gradle file includes the correct dependencies. For unit tests, you need testImplementation, while for instrumentation tests, you need androidTestImplementation.
dependencies {
// JUnit for unit tests
testImplementation "junit:junit:4.13.2"
// AndroidX test extensions
androidTestImplementation "androidx.test.ext:junit:1.1.5"
androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1"
}
After adding these dependencies, click on Sync Project with Gradle Files in Android Studio.
3. Writing a Simple Kotlin JUnit Test
Let’s start with a simple example. Suppose you have a utility class Calculator with basic arithmetic operations. You want to test its add and subtract methods.
class Calculator {
fun add(a: Int, b: Int): Int = a + b
fun subtract(a: Int, b: Int): Int = a - b
}
Now, create a test class inside src/test/java/:
import org.junit.Assert.assertEquals
import org.junit.Test
class CalculatorTest {
private val calculator = Calculator()
@Test
fun testAddition() {
val result = calculator.add(5, 3)
assertEquals(8, result)
}
@Test
fun testSubtraction() {
val result = calculator.subtract(10, 4)
assertEquals(6, result)
}
}
If Android Studio recognizes this test correctly, you should see the green run icon beside each method. If not, continue with the next fixes.
4. Common Fixes for “JUnit Tests Not Recognized”
4.1. Check Gradle Configuration
Make sure your test source sets are correctly defined. In your build.gradle (app-level), add:
android {
sourceSets {
test.java.srcDirs += "src/test/java"
androidTest.java.srcDirs += "src/androidTest/java"
}
}
4.2. Use the Correct Annotations
Kotlin tests require @Test annotation from JUnit. Make sure you import:
import org.junit.Test
Not:
import org.testng.annotations.Test
4.3. Clear Cache and Restart
Sometimes the issue is caused by Android Studio cache. Try:
- Go to File > Invalidate Caches / Restart.
- Rebuild project using Build > Rebuild Project.
4.4. Ensure JDK Compatibility
JUnit requires at least Java 8. If your project is set to a lower version, update your gradle.properties or build.gradle:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
5. Example: Instrumentation Test in Kotlin
If you want to run Android-specific tests (UI tests), you need instrumentation tests under src/androidTest. Example:
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.myapp", appContext.packageName)
}
}
Here, you must run the test on an emulator or physical device, since it depends on the Android framework.
6. Troubleshooting Checklist
| Issue | Possible Cause | Solution |
|---|---|---|
| Test class not recognized | Wrong directory | Place in src/test/java or src/androidTest/java |
| No tests found | Missing JUnit dependency | Add testImplementation "junit:junit:4.13.2" |
| Red underline on @Test | Wrong import | Use org.junit.Test not org.testng |
| Tests fail to run | JDK compatibility | Ensure jvmTarget = "1.8" |
| Gradle sync issues | Cache corruption | Invalidate Caches & Restart |
7. Conclusion
When you encounter Kotlin JUnit tests not recognized in Android Studio, don’t panic. Most of the time, the issue lies in dependency setup, wrong annotations, or misconfigured Gradle source sets. By applying the fixes in this article, you can quickly resolve the issue and continue writing effective unit and instrumentation tests for your Android apps.
For further reading on unit testing in Android, check the official documentation: Android Unit Testing Guide.