-
Notifications
You must be signed in to change notification settings - Fork 4k
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
LeakCanary in Unit Test Artifact #143
Comments
No. You just need to read the code to figure it out. It's because you're using Robolectric, which doesn't set a Instead, you can let your application class have a method that can be overridden in unit tests:
Then you just need to create a
|
TODO: Update README to mention Robolectric |
You can also disable leakCanary for unit tests: testCompile "com.squareup.leakcanary:leakcanary-android-no-op:${leakCanaryVersion}" |
If your tests run in debug mode like mine, to disable LeakCanary you'll need your // WORKS
androidTestCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
// DOESN'T WORK
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
androidTestCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1' Ran into this issue today so thought I'd share in case anyone else is confused |
Propose to close based on PR above |
@ygnessin @shubhamchaudhary I don't recall: which one works by default on a new Android project, |
@pyricau I think |
thx! |
Add indication to using no-op in unit test | #143
@ygnessin This works for me in all of my projects:
|
The readme now has this:
So we can close this issue. |
installing leakcanary causes the robolectric unit tests to throw a `NullPointerException` when running from within android studio (running from the command-line seem fine). as a, hopefully temporary, work-around the leakcanary installation is excluded when running the unit tests. ref: square/leakcanary#143 (comment)
I'm seeing this NPE now in unit tests, that I've updated my environment (android gradle plugin 3.0.0) I have this in my gradle file:
I tried changing the order of these statements, but so far haven't had luck getting rid of the NPE in unit tests, by trying to use the no-op version of the lib for tests. Note: the same issue occurs if i keep the old Note: tests run fine from within Android Studio. The error occurs running tests on the command line with Update: ok, if I RTFM and create a test application class, and override the However, it would be nice to just not use leak canary in tests by using the |
As per @JakeWharton's #907 PR, it suggests to do the following without subclassing Taken from README.md
|
@Kisty Thanks for the answer! it works :) |
Adding @JakeWharton's suggestion still causes NPEs in my setup. I added the config lines after the dependencies block in my app module's build.gradle, as well as adding the no-op version directly using
|
@Gloix can you post your dependency configuration with just the leak canary dependencies + the snippet Jake posted? Perhaps somethings in the wrong order. |
@Kisty Nevermind. After a couple of rebuilds it is working now :D, but thanks anyway. |
@Kisty I don't know what happened yesterday but my setup failed again, but this time I went deeper and pinpointed the set of possible configurations my test is running on:
none of them contains the substring AndroidTest or UnitTest, but all of them contain the substring prodDebug. It seems my tests are not running with a test-like gradle config and I'm kind of lost as to what I can do to tell Android Studio to run my tests using a test-like gradle config. |
@Gloix Were you ever able to resolve this? Adding the configurations block fixed it for me, but only for running from the command line. If I run tests from within the IDE, it still doesn't use the no-op artifact, and the tests fail. |
@kschults I never tried running it through the command line. I fixed it by killing leakcanary until I need it again. |
@Kisty Here's a shortened version of my
Overriding my application in test works, but it would definitely be better if I could just use the no-op version. I've looked at the dependency graph generated by |
Try swapping the dependency line for |
No luck. Same issue |
Ok, how about trying the Canary build of Android Studio (download and install separately http://d.android.com/studio/preview). I know the team are aware of unit test issues in Android Studio 3.1 with Robolectric. Do you have Robolectric in unit tests? |
Yeah, it's Robolectric. I'll give that a try (probably tomorrow) and get back to you |
@Kisty Unfortunately, I'm getting the same results on the Canary build (3.2 Beta 1) |
It is still the same issue in Android Studio 3.3 Canary 5 |
Having the same issues when upgrading from version 1.6.1. When I use 1.6.2 or 1.6.3 I get the NPE, and when debugging seems to be using the full artifact and not the |
OK, perhaps it's favouring the
|
@Kisty same result. I ran the test in console with |
@mikehc Sounds like the classpath resolution is different on AS from Gradle. Tried editing the run config so it uses JAR manifest or something else? Another few things to try from Android Studio:
|
@Kisty Can you give me some pointers of how to do that? |
I was wrong probably :) |
Hi.
I got NPE when i run test using Unit Test build variants. Do you have any best practice to do this ?
Is it caused by multiple flavour on release ?
The text was updated successfully, but these errors were encountered: