Dagger: Android Example with Mock providers in Android UI tests

Created on 1 Feb 2017  ·  12Comments  ·  Source: google/dagger

One of the benefits of having a dependency injection framework is the ability to replace the dependencies with different implementations.

This can potentially be used to provide a mock implementation of a class for UI tests.

Unfortunately, this is not well documented in repo and there are lots of different opinions on the internet.

An example project with a simple implementation would be really helpful.

This is a really simple blog post that does what I tried to describe:
https://artemzin.com/blog/jfyi-overriding-module-classes-with-dagger2/

dagger-android documentation

Most helpful comment

FWIW, https://google.github.io/dagger/testing.html says “don't do this!” about what you're doing.

All 12 comments

FWIW, https://google.github.io/dagger/testing.html says “don't do this!” about what you're doing.

@tbroyer that is just an example and although it says "don't do this!", it is listed as a possible option.

What I'm trying to achieve is to have a demo application which replaces implementations for "functional/integration/end-to-end testing". How it will be done is up to the people who implements/reviews/merges it.

Just like the documentation, it can even have sample that has a javaDoc comment that says that is not the best way to do it.

Create a test component that extends your application component and plugin test modules that return mocks.

I've used Espresso for UI testing in Android. If I need mocks for something in a production module I follow the "Organize modules for testability" section of the testing doc linked above. This typically results in adding two modules (ie NetworkModule, EspressoNetworkModule(includes NetworkModule), ProdNetworkModule(Includes NetworkModule)).

Thanks for an explanation how to do it @naturalwarren

The doc is there. I just thought that the repo may have an example.

@tasomaniac - I'd be happy to collaborate on an example. Why don't you write the "production" code and get as far as you can with the tests (ideally, write the tests that should fail and identify the points you'd like to mock) and I'll review it and offer suggestions from there.

@ronshapiro thanks for the reply. I'm not seeking an answer. I know how to do it. As I said, I opened this because I think someone can contribute to the repo by providing concrete examples on this subject. I believe that would be useful for many people.

I think someone can contribute to the repo by providing concrete examples on this subject

That person could be you! We'd love contributions!

Definitely. That was my plan. I'm somehow misunderstood. Thanks. :☺

@ronshapiro you still accepting contributions on this issue? I'd like to submit a PR for this. Of course, the sample I'd write would follow the guidelines outlined in https://google.github.io/dagger/testing.html.

My plan for swapping out the modules would be to create a build variant with a separate application subclass. It looks like you guys are using Bazel. Are you sure that's something you'd want to continue using in this sample?

If so, I'm happy to figure out how to use it for this, assuming its possible with Bazel. Just let me know.

Definitely still (and always!) accepting contributions for this. I was under the impression that Bazel does support manifest merging, but maybe I'm wrong? Either way, the application should be small enough that if the manifest needs to be duplicated, that's not so bad.

We're definitely sticking to bazel, but if there seems to be a large difference for an example for Gradle, we could entertain a side-by-side example. Let's start with Bazel first though.

As for test support in Bazel, I know it's being worked on (I'm on my phone, but you can search their issue tracker for android_test). It should be available soon AFAICT? Either way, I think the example code is what is most interesting, and not the build system, so if we need to ignore that, that's fine. We can test internally and be confident that when bazel does release Android test support, we'll be compatible

Fantastic.

You're right about the manifest merger. Thanks for the pointer to the Bazel testing support. I'll check that out.

I'll get to work then!

Hey @kmdupr33 did you end up getting to this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rciovati picture rciovati  ·  3Comments

blackberry2016 picture blackberry2016  ·  3Comments

Axrorxoja picture Axrorxoja  ·  3Comments

sagarwaghmare07 picture sagarwaghmare07  ·  3Comments

makaroffandrey picture makaroffandrey  ·  3Comments