Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio : Cannot use class from src in Unit tests

since the last Update (of Android Studio ? or Gradle ?) I cannot import code from prod in my tests classes.

A few months ago, the tests were working without any problems. I can launch the tests, using gradle in command line, or using AndroidStudio test runner. And it work nice : it passes when the code is ok, it fails when there is a bug.

But the auto-completion isn't working, the class, methods ... are red. I cannot click on an object to navigate to the declaration ... and so on.

And the AndroidStudio automatic quickfix is : "add dependency on module xxx" which add a line in the gradle file : implementation project(path: ':app')

I have the same problem on several project, with gradle 6.1.1 or 7.2.1

AndroidStudio version is : Chipmunk, 2021.2.1 patch 1 built on May 18 2022

the unit test, in app/src/test/java/com/apackage/ASimpleTest.kt

package com.apackage


import org.junit.Test
import kotlin.test.assertEquals

class ASimpleTest {

    @Test
    fun aTest(){

        val plop = AClass()
        assertEquals(1, plop.aMethod())
    }
}

the class in app/src/main/java/com/apackage/AClass.kt:

package com.apackage

class AClass {
    fun aMethod() = 1
}

I have no problem calling a method in the same folder as the tests. The problem is test/main folder calling src/main

like image 699
GaspardP Avatar asked Aug 14 '26 23:08

GaspardP


1 Answers

You got to add the 'src/test/java' and 'src/main/java' to the sourceSets in your app build.gradle

android {
    // Other configuration options...

    sourceSets {
        test {
            java.srcDirs = ['src/test/java', 'src/main/java']
        }
    }
}

like image 55
Abdullah Yahya Avatar answered Aug 18 '26 00:08

Abdullah Yahya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!