当前位置: 动力学知识库 > 问答 > 编程问答 >

NullPointerException from LeakCanary when running Robolectric tests

问题描述:

Added LeakCanary (1.3) to my Application:

@Override

public void onCreate() {

super.onCreate();

Fabric.with(this, new Crashlytics());

LeakCanary.install(this);

When I run the Robolectric test suite for my application I get a NullPointerException in LeakCanary.

Caused by: java.lang.NullPointerException

at com.squareup.leakcanary.LeakCanary.isInServiceProcess(LeakCanary.java:165)

at com.squareup.leakcanary.LeakCanary.isInAnalyzerProcess(LeakCanary.java:141)

at com.squareup.leakcanary.LeakCanary.install(LeakCanary.java:52)

at com.squareup.leakcanary.LeakCanary.install(LeakCanary.java:43)

at com.package.application.MyApplication.onCreate(MyApplication.java:50)

at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:131)

at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:431)

at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:224)

I added that Im using Crashlytics to point out that it (and other methods as well) receives the same Application but does not throw any Exceptions.

Wasn't sure if this should be here or on GitHub issues for LeakCanary. Anyone else experiencing this issue?

网友答案:

Converting my comment to the answer.

Robolectric provides way to deal with different initialisations and application test lifecycles through test application.

Here is your application class:

public class <YourAppplication> extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fabric.with(this, new Crashlytics());
        LeakCanary.install(this);
    }
}

You should put in test sources in the same package as yours application next class:

public class Test<YourAppplication> extends <YourApplication> {
    @Override
    public void onCreate() {
    }
}

Robolectric will load it instead of your application. As you can see I suppress all static initialisations from your application.

You can find more details here

网友答案:

A simple way to avoid the NullPointerException is to disable LeakCanary for unit tests by specifying the release (no-op) version in the testCompile directive in build.gradle. For instance:

dependencies {
    ...
    testCompile (
        'junit:junit:4.12',
        'org.robolectric:robolectric:3.0',
        'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
    )
    ...
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: