Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InjectFromComponent is not compatible with qualifiers #71

Open
masterpeter opened this issue Feb 22, 2018 · 2 comments
Open

InjectFromComponent is not compatible with qualifiers #71

masterpeter opened this issue Feb 22, 2018 · 2 comments

Comments

@masterpeter
Copy link

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!

@fabioCollini
Copy link
Owner

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

@moti0375
Copy link

moti0375 commented Feb 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants