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

No Dagger2 generated files for JUnit tests #125

Closed
westward opened this issue Feb 24, 2015 · 11 comments
Closed

No Dagger2 generated files for JUnit tests #125

westward opened this issue Feb 24, 2015 · 11 comments

Comments

@westward
Copy link

A new AndroidStudio 1.1 version introduced the unit testing support. This URL http://tools.android.com/tech-docs/unit-testing-support provides step-by-step instruction how to setup IDE to run JUnit tests for Android project.

android-apt plugin by Hugo Visser works nice in assisting of creation Dagger2 generated files for usual Android code but unfortunately there is no generated files for any JUnit test class. I tried to configure dependency like androidTestApt 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT' (and tried to specify 'testCompile' and 'provided' too) according to android-apt plugin documentation but without success.

I think the problem is in different sources directory for Unit tests - it's src/test/java instead of src/androidTest/java that used by android instrumentation tests.

Can you please provide any help how to resolve this trouble? I can't understand where is the problem: in the wrong project configuration, a bug in new Android Studio's JUnit tests feature, something wrong in Dagger2 or in android-apt plugin. When needed, I can provide the simple AndroidStudio project to reproduce the trouble.

Thanks in advance.

@stephanenicolas
Copy link

For me it works when adding dagger 2 with both apt and testCompile scopes.

BTW, it would be great if we could have an example of testing with Dagger 2. It's not obvious at all to figure out how module can be overiden.

@westward
Copy link
Author

Can you please provide an example project? I tried to add dagger compiler as a testCompile dependency but without success.

@twelve17
Copy link

twelve17 commented Mar 3, 2015

I'm also running into this issue. An example project or a few pointers would be helpful. Thanks!

@sockeqwe
Copy link

That build.gradle script works for me (with android apt plugin):

dependencies {
    apt 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT'
    compile 'com.google.dagger:dagger:2.0-SNAPSHOT'

    // Additional for unit tests
    testProvided 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT'
}

@westward
Copy link
Author

Can you please provide an example project? I would like to see the whole project configuration related to JUnit tests.

@cgruber
Copy link

cgruber commented Mar 26, 2015

Our recommendation for Android Studio is to use https://bitbucket.org/hvisser/android-apt as described above. I'm going to close this. If you are still encountering it when using the apt plugin, and after updating all dependencies, please re-open this, but point to either a github repo with an example project, or a gist containing a failing test, or something so we can reproduce this.

@cgruber cgruber closed this as completed Mar 26, 2015
@westward
Copy link
Author

Here is the AndroidStudio example project: https://github.com/westward/Dagger2Test
It contains the single JUnit file named DummyControllerTest.java with code that should create a Component. I tried "testProvided", "testCompile" directives in build.gradle, both variants without success.
Thanks!
P.S. Christian, I cannot re-open my issue. Please do it by yourself if it's possible.

@stephanenicolas
Copy link

@cgruber why use android apt plugin ? Gradle and Android Studio are supposed to have annotation processor support out of the box, no ?

@tbroyer
Copy link

tbroyer commented Mar 26, 2015

@stephanenicolas javac does annotation processing out of the box (unless told otherwise) by looking for processors in the classpath. So in that sense, yes, Gradle supports annotation processing out of the box. But Gradle is missing one thing: dependencies that you need when compiling but not at runtime (such as annotation processors; you don't want to package them into your application). Additionally, ideally, you'd want to be explicit about your processorpath, rather than just put annotation processors in the classpath: the processorpath is usually much smaller than the classpath so processor discovery is faster, but more importantly you know you're not bringing in an annotation processor that you didn't mean to use (for example, antlr' includes its own annotation processor: antlr/antlr4#487)

So you want an apt (or whatever its name) configuration that ends up filling javac's -processorpath, independently of the classpath. android-apt does just that, for Android builds.

@JakeWharton
Copy link

And it also makes the generated sources visible to the IDE automatically.

@westward
Copy link
Author

OK guys, what's wrong with my example project? What I've missing? I can't use Dagger2 in tests, I didn't configured project well, something else? Any advice or hint please!

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

7 participants