我试图在我目前的android项目中编写简单的集成测试,该项目完全用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
我试着用谷歌搜索这个问题,但没有成功.
我已经try 了以下步骤:
- 判断是否安装了包含InstantTaskExecutorRule的库,我可以查看此软件包(是的,我可以)
- 判断我的测试是否放在正确的文件夹中(是的,它在
androidTest
中) - 判断我是否正确启动了测试(可能是通过右键单击Package
pkgName (androidTest)
,然后"Run test 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.
但我想用这个特殊的InstantTaskExecutorRule
,想知道问题到底是什么,以及如何解决它,或者至少想知道我应该在哪里寻找什么.
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()
}
}