刀具 - 单位测试
我想开始在测试中使用刀柄。
gradle:
android{
...
defaultConfig{
...
testInstrumentationRunner "com.rachapps.myfitapp.HiltTestRunner"
}
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'
...
class HiltTestRunner : AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
测试课:
@SmallTest
@HiltAndroidTest
class BodyPartDaoTest {
@get : Rule
var hiltRule = HiltAndroidRule(this)
@get : Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Inject
@Named("test_db")
lateinit var database: MyFitDatabase
private lateinit var bodyPartDao: BodyPartDao
private lateinit var exerciseDao: ExerciseDao
@Before
fun setup() {
hiltRule.inject()
bodyPartDao = database.bodyPartDao()
exerciseDao = database.exerciseDao()
}
...
}
模块:
@Module
@InstallIn(SingletonComponent::class)
object TestAppModule {
@Provides
@Named("test_db")
fun provideInMemoryDb(@ApplicationContext context: Context) : MyFitDatabase {
return Room.inMemoryDatabaseBuilder(context, MyFitDatabase::class.java).allowMainThreadQueries().build()
}
}
执行测试时,我会收到一个错误:
C:\Radek\Android\MyFitApp\app\build\generated\source\kapt\debugAndroidTest\com\rachapps\myfitapp\data\dao\BodyPartDaoTest_TestComponentDataSupplier.java:14: error: BodyPartDaoTest_TestComponentDataSupplier is not abstract and does not override abstract method get() in TestComponentDataSupplier
public final class BodyPartDaoTest_TestComponentDataSupplier extends TestComponentDataSupplier {
^
C:\Radek\Android\MyFitApp\app\build\generated\source\kapt\debugAndroidTest\com\rachapps\myfitapp\data\dao\BodyPartDaoTest_TestComponentDataSupplier.java:15: error: get() in BodyPartDaoTest_TestComponentDataSupplier cannot override get() in TestComponentDataSupplier
protected TestComponentData get() {
^
return type TestComponentData is not compatible with Map<Class<?>,TestComponentData>
2 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugAndroidTestJavaWithJavac'.
I would like to start using Hilt in my test.
Gradle:
android{
...
defaultConfig{
...
testInstrumentationRunner "com.rachapps.myfitapp.HiltTestRunner"
}
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'
...
class HiltTestRunner : AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
test class:
@SmallTest
@HiltAndroidTest
class BodyPartDaoTest {
@get : Rule
var hiltRule = HiltAndroidRule(this)
@get : Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Inject
@Named("test_db")
lateinit var database: MyFitDatabase
private lateinit var bodyPartDao: BodyPartDao
private lateinit var exerciseDao: ExerciseDao
@Before
fun setup() {
hiltRule.inject()
bodyPartDao = database.bodyPartDao()
exerciseDao = database.exerciseDao()
}
...
}
Module:
@Module
@InstallIn(SingletonComponent::class)
object TestAppModule {
@Provides
@Named("test_db")
fun provideInMemoryDb(@ApplicationContext context: Context) : MyFitDatabase {
return Room.inMemoryDatabaseBuilder(context, MyFitDatabase::class.java).allowMainThreadQueries().build()
}
}
While executing test I receive an error:
C:\Radek\Android\MyFitApp\app\build\generated\source\kapt\debugAndroidTest\com\rachapps\myfitapp\data\dao\BodyPartDaoTest_TestComponentDataSupplier.java:14: error: BodyPartDaoTest_TestComponentDataSupplier is not abstract and does not override abstract method get() in TestComponentDataSupplier
public final class BodyPartDaoTest_TestComponentDataSupplier extends TestComponentDataSupplier {
^
C:\Radek\Android\MyFitApp\app\build\generated\source\kapt\debugAndroidTest\com\rachapps\myfitapp\data\dao\BodyPartDaoTest_TestComponentDataSupplier.java:15: error: get() in BodyPartDaoTest_TestComponentDataSupplier cannot override get() in TestComponentDataSupplier
protected TestComponentData get() {
^
return type TestComponentData is not compatible with Map<Class<?>,TestComponentData>
2 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugAndroidTestJavaWithJavac'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定异常的原因是什么,但是您向测试提供内存数据库的方式并不是文档所说的您应该做的。
尝试替换绑定,看看会发生什么。
I am not sure as to what is the reason for the exception, but the way you are providing in-memory DB to the test, is not what the documentation says you should do.
Try replacing the binding, and see what happens.
尝试保持 Hilt 库版本相同
Try with keeping Hilt library versions same
由于过时的库:com.google.dagger:Hilt-android测试,我遇到了这个问题
I had that problem because of outdated library: com.google.dagger:hilt-android-testing