Added LeakCanary (1.3) to my Application:
在我的应用程序中添加了LeakCanary(1.3):
@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.
当我为我的应用程序运行Robolectric测试套件时,我在LeakCanary中得到一个NullPointerException。
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.
我补充说,我使用Crashlytics指出它(以及其他方法)接收相同的应用程序但不抛出任何异常。
Wasn't sure if this should be here or on GitHub issues for LeakCanary. Anyone else experiencing this issue?
不确定这应该在这里还是在LeakCanary的GitHub问题上。有其他人遇到过这个问题吗?
2 个解决方案
#1
Converting my comment to the answer.
将我的评论转换为答案。
Robolectric
provides way to deal with different initialisations and application test lifecycles through test application.
Robolectric提供了通过测试应用程序处理不同初始化和应用程序测试生命周期的方法。
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.
Robolectric将加载它而不是您的应用程序。如您所见,我会抑制应用程序中的所有静态初始化。
You can find more details here
你可以在这里找到更多细节
#2
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:
避免NullPointerException的一种简单方法是通过在build.gradle中的testCompile指令中指定release(no-op)版本来禁用LeakCanary以进行单元测试。例如:
dependencies {
...
testCompile (
'junit:junit:4.12',
'org.robolectric:robolectric:3.0',
'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
)
...
}
#1
Converting my comment to the answer.
将我的评论转换为答案。
Robolectric
provides way to deal with different initialisations and application test lifecycles through test application.
Robolectric提供了通过测试应用程序处理不同初始化和应用程序测试生命周期的方法。
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.
Robolectric将加载它而不是您的应用程序。如您所见,我会抑制应用程序中的所有静态初始化。
You can find more details here
你可以在这里找到更多细节
#2
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:
避免NullPointerException的一种简单方法是通过在build.gradle中的testCompile指令中指定release(no-op)版本来禁用LeakCanary以进行单元测试。例如:
dependencies {
...
testCompile (
'junit:junit:4.12',
'org.robolectric:robolectric:3.0',
'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
)
...
}