34

I'm trying to write simple integration test in my current android project written totally in Kotlin.

The problem is that the test doesn't even start and fails with the following error:

Error:(4, 36) Unresolved reference: testing
Error:(18, 52) Unresolved reference: InstantTaskExecutorRule
Error:Execution failed for task ':app:kaptGenerateStubsDebugAndroidTestKotlin'.
> Compilation error. See log for more details

I've tried googling around this problem but without success.

Steps I already tried to folow:

  1. Check if the library that contains InstantTaskExecutorRule is installed and I can dive into this package (yes, I can)
  2. Check if my test is placed in a correct folder (yes, it's in the androidTest)
  3. Check if I launch my tests properly (probably, I'm launching them by right click package pkgName (androidTest) and then "Run tests in...")

I've also tried to rename my source dirs to koltin from java, and set proper values to sourceSets but changed it back because of no success as well and considered it's not a reason.

Important notice:

If I comment the line import android.arch.core.executor.testing.InstantTaskExecutorRule and all code, related to InstantTaskExecutorRule (meaning that the whole test logic will be empty) and place a simple assert for example, then everything works just fine.

But I want to use this particular InstantTaskExecutorRule and would like to know what the problem actually is and how to solve it, or at least to know where should I look for and what.

Here is my test code:

import android.arch.core.executor.testing.InstantTaskExecutorRule
import android.arch.persistence.room.Room
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4

import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import com.myapp.storage.base.AppDataBase


@RunWith(AndroidJUnit4::class)
class UserDaoTest{
    @JvmField @Rule val instantTaskExecutorRule = InstantTaskExecutorRule()

    private lateinit var db: AppDataBase

    @Before
    @Throws(Exception::class)
    fun setUp(){
        db = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), AppDataBase::class.java)
                .allowMainThreadQueries()
                .build()
    }

    @After
    fun closeDB(){
        db.close()
    }

    @Test
    fun getUsersWhenNoUserInserted(){
        db.userDao().allUsers()
                .test().assertNoValues()
    }

}
vendettacore
  • 1,439
  • 1
  • 13
  • 28

4 Answers4

38

According to official google documentation we should add our test helpers for Architecture Components (LiveData) in a such way:

// Test helpers for LiveData
testImplementation "android.arch.core:core-testing:1.1.0"

And at least for me it just doesn't work. (see the question above)

How it should be actually:

// Test helpers for LiveData
androidTestImplementation "android.arch.core:core-testing:1.1.0"

Now everything works just fine!

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
vendettacore
  • 1,439
  • 1
  • 13
  • 28
  • Thanks for posting answer. using "androidTestImplementation" was my solution as well with a different library. Very weird. – The Tokenizer Mar 26 '18 at 04:28
  • 1
    I'm having the same issue @vendettacore, when using `import androidx.test.platform.app.InstrumentationRegistry` within the **test** directory for a JUnit test. The error is '_Unresolved reference: test_'. – AdamHurwitz Aug 11 '19 at 22:41
  • 1
    Here is the [solution](https://stackoverflow.com/a/57454390/2253682) to my issue above with the **import**. It was related to how the library dependency is added. – AdamHurwitz Aug 12 '19 at 00:16
  • 2
    I thought it was testImplementation or androidTestImplementation depending on which directory of the test (instrumental or not) needs the dependency. This means you might need both. Can someone confirm or deny that? – Quentin vk Feb 19 '20 at 14:47
  • I did it for all test dependencies and the error is gone now. – GAMA May 30 '21 at 04:37
  • What @Lawnio mentions is what worked for me and makes more sense. I was using testImplementation while having the actual test in the directory corresponding to instrumentation tests. Moving the test to the correct folder solved it. – Miguel Lasa Jun 18 '21 at 00:17
9

I just faced the same issue, and it was because I imported a newer version of junit, 4.13-beta-3. After downgrade to version 4.12, everything was fine.

testImplementation "junit:junit:4.12"

I hope this can be helpful for others.

Paul Wang
  • 157
  • 2
  • 3
0

For me the specific issue was related to the Unresolved reference: InstantTaskExecutorRule I solved it upgrading, in dependencies brackets,

    testImplementation "androidx.arch.core:core-testing:2.1.0"

from 2.0.0 to 2.1.0 Bye bye

Dharman
  • 30,962
  • 25
  • 85
  • 135
Lucas
  • 458
  • 4
  • 6
0

I had to add the following to clear this error in a KMP project running tests

dependencies {
    implementation(kotlin("test"))
    implementation(kotlin("test-junit"))
}

This is in the root of the gradle file, not in a test project

bsautner
  • 4,479
  • 1
  • 36
  • 50