DaggerMock
DaggerMock copied to clipboard
InjectFromComponent is not compatible with qualifiers
Hi Fabio! I'm trying to do something like this:
class MyTest {
@Rule [...DaggerMockRule with MyComponent...]
@InjectFromComponent @Dog Animal a1;
@InjectFromComponent @Cat Animal a2;
fun `test inject from component with qualifier` {
assertTrue(a1 is Dog)
assertTrue(a2 is Cat)
}
}
The second assert will fail as a2 is again a Dog. In MyComponent I'm correctly exposing both Animal with their respective qualifiers. It seems that here the first method encountered which expose the type Animal will return, thus leading to the defect.
This doesn't seem to happen when using DaggerMockRule setter without using InjectFromComponent
Am I wrong? Thank you so much for your help, good job!
Hi, you are right, this use case wasn't supported. But it was quite simple to add the code to support it, can you try to use the commit version db3a68609a? Many thanks for your report
Hi, I am trying this on version 0.8.5 but it doesn't seems to work.. Our dagger version is 2.47 My Component:
`@Component(modules = [TestModule::class])
interface TestComponent {
@Component.Builder
interface Builder {
fun build(): TestComponent
fun testModule(testModule: TestModule): TestComponent.Builder
}
@String1 fun string1() : String
@String2 fun string2() : String
}`
My Module with qualifiers:
`@Module
class TestModule {
@Provides
@String1
fun provideString1() : String = "String1"
@Provides
@String2
fun provideString2() : String = "String2"
}
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class String1
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class String2`
And test:
`@RunWith(RobolectricTestRunner::class)
class StamTest {
@get:Rule
val mockRule: DaggerMockRule<TestComponent> = DaggerMockRule(
TestComponent::class.java, TestModule()
)
@InjectFromComponent
@String1
lateinit var string1: String
@InjectFromComponent
@String2
lateinit var string2: String
@Test
fun qualifiersTest(){
println("String1: $string1, String2: $string2")
assert(string1 == "String1")
assert(string2 == "String2") //This one fails, it injects String1 instead
}
}`
Help appreciated, Moti