free geoip
47

Fix: Unit Tests Not Running in Kotlin Android Studio

When working on Android development using Kotlin, writing unit tests is essential to ensure code reliability and prevent bugs before…

When working on Android development using Kotlin, writing unit tests is essential to ensure code reliability and prevent bugs before your app hits production. However, many developers encounter a frustrating issue: unit tests not running in Android Studio, especially when using Kotlin.

If you’re facing this problem, don’t worry. This guide walks you through the common causes and solutions for unit tests that fail to run or appear greyed out in Android Studio. By the end, you’ll be equipped with working examples and project configurations to resolve the issue completely.

Common Symptoms

  • The test class is not detected by Android Studio.
  • The “Run” or “Debug” icons don’t appear next to the test functions.
  • Running the test manually results in No tests found.
  • Android Studio shows “Test events were not received”.

Typical Causes

Here are the main reasons your Kotlin unit tests might not be running:

  1. Missing or misconfigured test dependencies
  2. Incorrect directory structure for test classes
  3. Missing @Test annotation or using JUnit4 vs JUnit5 improperly
  4. Gradle configuration issues
  5. Using AndroidJUnit4 runner for unit tests instead of instrumentation tests

Example Problem: Test Not Running

Let’s consider a simple function and test case:

1. Main Kotlin Function

// File: app/src/main/java/com/example/utils/Calculator.kt
package com.example.utils

class Calculator {
    fun add(a: Int, b: Int): Int = a + b
}

2. Test Case (Not Running)

// File: app/src/test/java/com/example/utils/CalculatorTest.kt
package com.example.utils

import org.junit.Test
import org.junit.Assert.assertEquals

class CalculatorTest {
    @Test
    fun testAdd() {
        val calculator = Calculator()
        val result = calculator.add(2, 3)
        assertEquals(5, result)
    }
}

Issue: When you try to run this test, Android Studio might not detect it or say “No tests found”.

Solution Steps

1. Check Directory Structure

Your unit test should be in:

app/src/test/java/com/example/utils/CalculatorTest.kt

❗ Don’t put unit tests under androidTest unless you’re writing instrumentation tests.

2. Apply the Correct Dependencies

Ensure your build.gradle includes these dependencies for JUnit4 support:

// app/build.gradle
dependencies {
    testImplementation 'junit:junit:4.13.2'
}

If using JUnit5, you’ll need:

testImplementation("org.junit.jupiter:junit-jupiter:5.9.3")

test {
    useJUnitPlatform()
}

3. Use @Test Annotation Correctly

import org.junit.Test  // Make sure it's this one!

Avoid importing incorrect annotations from other libraries or frameworks.

4. Set the Test Runner Correctly (Optional)

If you’re running instrumentation tests, use AndroidJUnit4. For unit tests, you don’t need a test runner. If used improperly, the test will not run.

Wrong:

@RunWith(AndroidJUnit4::class)

Correct (for unit test):

// No @RunWith needed for standard unit tests

5. Rebuild and Invalidate Caches

If everything seems correct but tests still don’t run:

  • Go to File > Invalidate Caches / Restart
  • Then Clean Project and Rebuild

Working Example That Runs Successfully

// File: app/src/test/java/com/example/utils/CalculatorTest.kt
package com.example.utils

import org.junit.Test
import org.junit.Assert.assertEquals

class CalculatorTest {

    @Test
    fun testAddition() {
        val calculator = Calculator()
        assertEquals(5, calculator.add(2, 3))
    }
}

Run it: Right-click on the class or method > Run ‘CalculatorTest’

Bonus: Troubleshooting Table

ProblemCauseSolution
Test class not foundWrong folder or missing annotationUse @Test and put file under src/test/java
No tests foundJUnit5 used without platformAdd useJUnitPlatform() in build.gradle
Test runs as instrumentation testUsing @RunWith(AndroidJUnit4::class)Remove annotation for unit tests
Build fails on testMissing dependenciesAdd correct testImplementation

Final Tips

  • Always use src/test for unit tests and src/androidTest for UI/instrumentation tests.
  • Ensure that the Kotlin compiler and Android Gradle plugin versions are up-to-date.
  • Consider switching to JUnit5 if you want more flexible parameterized tests or nested classes.

Summary

Unit tests are a vital part of Android app development. When tests don’t run in Kotlin projects inside Android Studio, it’s usually due to project structure issues or missing configurations. This article covered common causes, fixes, and working examples to help you run tests properly.

By resolving these issues, you ensure your codebase remains clean, maintainable, and free from critical bugs making you a more efficient and professional Android developer.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *