Skip to content

Add espresso example to demo swapping dependenices for espresso tests#742

Open
kmdupr33 wants to merge 4 commits intogoogle:masterfrom
kmdupr33:add_espresso_example
Open

Add espresso example to demo swapping dependenices for espresso tests#742
kmdupr33 wants to merge 4 commits intogoogle:masterfrom
kmdupr33:add_espresso_example

Conversation

@kmdupr33
Copy link
Copy Markdown

This addresses #571 and #564

Currently, it doesn't look like there's support for instrumentation tests with Bazel (see the commit message here, so as @ronshapiro suggested here, I'm not going to worry about that part. Moreover, the manifest merger doesn't appear to work as advertised, so there is some duplication between main and stubbed variant manifests.

android:targetSdkVersion="24"/>

<application
android:label="Espresso Dagger"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just "Instrumentation Dagger"?

Espresso is a concrete library for Instrumentation UI tests which not everybody use and not only it does not matter for this sample but it's also just an implementation detail.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to that. Depends on where folks want to go with this sample.

I imagined that we might eventually want to demo best practices for solving some espresso-specific problems with Dagger. For example, we might want to show how to setup modules and components so that you can inject dependencies that implement IdlingResource for a test build variant. Maybe that's a bad example, but if we want to show any espresso specific stuff, then maybe the name should stay.

import dagger.example.android.espresso.main.ApiClient;

@Module
public class StubNetworkingModule {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why redeclare module instead of extending and overriding only those @Provides methods that you need?

Copy link
Copy Markdown
Author

@kmdupr33 kmdupr33 May 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overriding modules is not recommended by the testing guide. https://google.github.io/dagger//testing.html

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is an anti-pattern as it requires the real dependencies of that provider be available and it prevents adding test-only dependencies for the test version of the provider. Composing modules is much more powerful and is the recommended practice.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but mostly overriding is completely fine while redeclaring requires either duplicating other @Provides methods or creating modules just for one dependency.

Obviously composition is preferred over inheritance, but there is cost to each decision.

Anyway, this seems covered pretty well in docs now so let's stick to "recommended" practice.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bump. is this going to get merged?

@jaredsburrows
Copy link
Copy Markdown

@ronshapiro What needs to be done to get this example in? It would be nice to see this for people that would like to migrate to Bazel.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants