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:
androidTest
)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()
}
}
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!