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

Avoiding duplication in instrumentation tests with Hilt #1896

Closed
luis-cortes opened this issue Jun 10, 2020 · 2 comments
Closed

Avoiding duplication in instrumentation tests with Hilt #1896

luis-cortes opened this issue Jun 10, 2020 · 2 comments

Comments

@luis-cortes
Copy link

luis-cortes commented Jun 10, 2020

My previous testing setup used separate component configurations to swap out production bindings for testing ones. Since the swapping of bindings was happening in TestApplicationComponent I never had to worry about doing this on each individual test. This was nice.

Currently, all of my UI tests extend BaseTest which runs rules common to all UI tests (I've also moved some ApplicationComponent bindings that used to be injected into my TestApp to BaseTest since the new recommendation is to not use @CustomTestApplication).

BaseTest seemed like a natural place to put @UninstallModules and @HiltAndroidTest.
However, doing so leads to an IllegalStateException because MarkThatRulesRanRule expects the runtime class to be annotated with @HiltAndroidTest.

The suggested approach seems to be adding @UninstallModules and @HiltAndroidTest to each test. I would really like to avoid this for 2 reasons:

  • Adding or deleting a module from every test is error prone and time consuming.
  • It would be nice not to have to add the annotations, rule, fields to be injected, and the actual call to inject to all these tests.

Is avoiding this duplication possible with Hilt?

@bcorso
Copy link

bcorso commented Jun 10, 2020

Hi Luis, thanks for the feed back!

Adding or deleting a module from every test is error prone and time consuming.

Yep, totally agree. We have plans to improve this part of the API, although how exactly we'll address it hasn't been decided yet (i.e. whether it will be through base test class or some other mechanism).

Hopefully we can find some balance between easily setting up bindings across multiple tests but also allowing each test to be flexible enough to configure itself differently from other tests.

It would be nice not to have to add the annotations, rule, fields to be injected,
and the actual call to inject to all these tests.

Most of this should work, e.g.

public abstract class BaseTest {

  @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this);

  @Inject Foo foo;

  @Before
  public final void baseSetup() {
    rule.inject();
  }
}

Note that the test must still be annotated with @HiltAndroidTest, but I think that's less of an issue since the annotation doesn't contain any state itself.

@HiltAndroidTest
public class MyTest extends BaseTest {
  ...
}

@luis-cortes
Copy link
Author

Thanks for the prompt reply!

... allowing each test to be flexible enough to configure itself differently from other tests.

For what it's worth, I think this added flexibility is great.

Note that the test must still be annotated with @HiltAndroidTest, but I think that's less of an issue since the annotation doesn't contain any state itself.

Agreed. This is much less of an inconvenience.

Thanks for the heads up on the viability of BaseTest. I'll have to move @UninstallModules to all the subclasses for now, but not having to duplicate the injection, fields, and rule helps a ton.

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

2 participants