刀具 - 单位测试

发布于 2025-01-19 01:34:30 字数 2552 浏览 0 评论 0原文

我想开始在测试中使用刀柄。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单调的奢华 2025-01-26 01:34:30

我不确定异常的原因是什么,但是您向测试提供内存数据库的方式并不是文档所说的您应该做的。

尝试替换绑定,看看会发生什么。

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.

此刻的回忆 2025-01-26 01:34:30

尝试保持 Hilt 库版本相同

//Hilt testing
    androidTestImplementation 'com.google.dagger:hilt-android-testing:2.37'
    kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.37'
//hilt dependency
    kapt "com.google.dagger:hilt-compiler:2.37"
    implementation("com.google.dagger:hilt-android:2.37") 

Try with keeping Hilt library versions same

//Hilt testing
    androidTestImplementation 'com.google.dagger:hilt-android-testing:2.37'
    kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.37'
//hilt dependency
    kapt "com.google.dagger:hilt-compiler:2.37"
    implementation("com.google.dagger:hilt-android:2.37") 
白首有我共你 2025-01-26 01:34:30

由于过时的库:com.google.dagger:Hilt-android测试,我遇到了这个问题

I had that problem because of outdated library: com.google.dagger:hilt-android-testing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文