8000 Dependencies not provided in tests. · Issue #1956 · google/dagger · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Dependencies not provided in tests. #1956

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

Closed
Kotlinovsky opened this issue Jun 26, 2020 · 10 comments
Closed

Dependencies not provided in tests. #1956

Kotlinovsky opened this issue Jun 26, 2020 · 10 comments

Comments

@Kotlinovsky
Copy link
Kotlinovsky commented Jun 26, 2020

Hi! I tried to use Hilt, and had a problem during testing

image
image
image
image
image

APPLICATION and COUNTRIES-FEATURE modules contain in INJECTOR Gradle-module.

@bcorso
Copy link
bcorso commented Jun 26, 2020

Hi Maxim, what was the actual error you ran into? Can you post it here?

@Kotlinovsky
Copy link
Author

Hi Maxim, what was the actual error you ran into? Can you post it here?

Dependencies injected to test class by rule.inject(), but not injected in View-class.

@bcorso
Copy link
bcorso commented Jun 26, 2020

The activity that hosts the view also needs to be annotated with @AndroidEntryPoint. An easy way to do this is to create a test activity nested in your test:

  @AndroidEntryPoint
  public static final class TestActivity extends AppCompatActivity {}

  ...

  controller = Robolectric.buildActivity(TestActivity.class);

@Kotlinovsky
Copy link
Author
Kotlinovsky commented Jun 26, 2020

image
image
It's not working ;( In activity dependency also not provided.

@Kotlinovsky
Copy link
Author

image
It's also not fixing my trouble.

@Kotlinovsky
Copy link
Author
Kotlinovsky commented Jun 26, 2020

I checked instrumental tests with Hilt and dependencies successfully injected. But in local tests injection doesn't work.

@bcorso
Copy link
bcorso commented Jun 26, 2020

Yep, I was able to reproduce your issue.

Turns out our bytecode injected classes do not get used when running Robolectric from AndroidStudio (via the play button). Things work when testing via ./gradlew test, but AndroidStudio uses a custom pipeline for running its test.

There's a couple work arounds:

  1. Run the test from the terminal using Gradle rather than AndroidStudio, e.g. ./gradlew test

  2. Use the long-form notation for all @AndroidEntryPoint annotated classes. This will avoid rely on bytecode injection. For example:

    @AndroidEntryPoint(AppCompatActivity.class)
    public static final class MyActivity extends Hilt_MyActivity {...}

Edit: Also see Dany's suggestion below: #1956 (comment)

Thanks for reporting this! I've made a note of this in the testing documentation.

@Kotlinovsky
Copy link
Author

Thanks for supporting!

@danysantiago
Copy link
Member

I wanted to add, that this is a known issue we are working with the Android Gradle Plugin team to fix, specifically b/37076369.

The bytecode transform is done but when running tests from Android Studio the IDE does not grab the transformed classes, which causes injection not to occur in @AndroidEntryPoint-annotated classes in the test sources and when running tests from Android Studio (via the play button in the test method or class). Note that this only occurs for host-side tests, i.e. Robolectric not instrumentation tests. Meanwhile, if executed from command line it should work.

Alternatively you can configure Android Studio to execute tests via the Gradle test task instead of its internal task.

  • Create a new 'Run Configuration' of type 'Gradle'
  • Gradle project should be the Gradle module where the tests are.
  • Task is usually testDebug
  • Arguments should be --tests MyTestClass

Here is an example:
Screen Shot 2020-06-26 at 11 13 42 AM

We'll update the docs to mention the workarounds while a fix is being worked on.

This was referenced Jun 30, 2020
kevinb9n pushed a commit that referenced this issue Jul 6, 2020
#1956

Link:
[]

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=319808706
kevinb9n pushed a commit that referenced this issue Jul 6, 2020
#1956

Link:
[]

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=319808706
@bcorso bcorso reopened this Jul 30, 2020
@bcorso bcorso closed this as completed Nov 5, 2020
@otaviosgoncalves
Copy link

This bug still remains....

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

No branches or pull requests

4 participants
0