Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference: testing

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()
    }

}
like image 270
vendettacore Avatar asked Feb 23 '18 14:02

vendettacore


People also ask

How to fix Unresolved reference Android Studio?

If you still see the unresolved reference error after fixing the problem, try to build your Android application with Command + F9 for Mac or Control + F9 for Windows and Linux. The error should disappear after the build is completed.

What is unresolved reference in Kotlin?

If you have Kotlin file within the same package and put there some classes and missed the package declaration it looks inside the IntelliJ that you have classes in the same package but without definition of package the IntelliJ shows you: Error:(5, 5) Kotlin: Unresolved reference: ...

What is InstantTaskExecutorRule?

android.arch.core.executor.testing.InstantTaskExecutorRule. A JUnit Test Rule that swaps the background executor used by the Architecture Components with a different one which executes each task synchronously. You can use this rule for your host side tests that use Architecture Components.


2 Answers

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!

like image 187
vendettacore Avatar answered Oct 06 '22 14:10

vendettacore


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.

like image 9
Paul Wang Avatar answered Oct 06 '22 14:10

Paul Wang