所以我正在创建一个使用这个库https://github.com/OmerUygurOzer/Knit(我创建的东西)的示例项目。
问题是测试不会随之运行。不知道为什么。该应用程序本身工作正常。但是当我运行测试时,它在错误的行上就会在Application类中调用Knit.init(this);
。
这是logcat
02-13 20:46:20.471 23727-23727/com.omerozer.knitmvpexample W/System.err: java.lang.NoSuchMethodException: <init> [interface com.omerozer.knit.InternalModel]
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at java.lang.Class.getConstructor(Class.java:531)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at java.lang.Class.getConstructor(Class.java:495)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at com.omerozer.knit.KnitClassLoader.getViewToPresenterConstructor(KnitClassLoader.java:64)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at com.omerozer.knit.KnitClassLoader.getViewToPresenterMap(KnitClassLoader.java:79)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at com.omerozer.knit.KnitClassLoader.<init>(KnitClassLoader.java:23)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at com.omerozer.knit.Knit.init(Knit.java:28)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at com.omerozer.knitmvpexample.MVPApp.onCreate(MVPApp.java:16)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1012)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at android.support.test.runner.MonitoringInstrumentation.callApplicationOnCreate(MonitoringInstrumentation.java:382)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4553)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at android.app.ActivityThread.access$1500(ActivityThread.java:151)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at android.os.Looper.loop(Looper.java:135)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at java.lang.reflect.Method.invoke(Native Method)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-13 20:46:20.472 23727-23727/com.omerozer.knitmvpexample W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
02-13 20:46:20.473 23727-23727/com.omerozer.knitmvpexample E/InstrumentationResultPrinter: Failed to mark test No Tests as finished after process crash
02-13 20:46:20.473 23727-23727/com.omerozer.knitmvpexample E/MonitoringInstr: Exception encountered by: com.omerozer.knitmvpexample.MVPApp@3a68f1a5. Dumping thread state to outputs and pining for the fjords.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[])' on a null object reference
at com.omerozer.knit.KnitClassLoader.getViewToPresenterMap(KnitClassLoader.java:79)
at com.omerozer.knit.KnitClassLoader.<init>(KnitClassLoader.java:23)
at com.omerozer.knit.Knit.init(Knit.java:28)
at com.omerozer.knitmvpexample.MVPApp.onCreate(MVPApp.java:16)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1012)
at android.support.test.runner.MonitoringInstrumentation.callApplicationOnCreate(MonitoringInstrumentation.java:382)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4553)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
错误信息是这样的:
Started running tests
Test running failed: Instrumentation run failed due to java.lang.NullPointerException
好像测试环境找不到init方法?这对我没有意义。
这是gradle设置。
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.omerozer.knitmvpexample"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'com.github.OmerUygurOzer.knit:knitlib:v1.0.2'
androidTestAnnotationProcessor 'com.github.OmerUygurOzer.knit:knitprocessor:v1.0.2'
implementation 'com.github.OmerUygurOzer.knit:knitlib:v1.0.2'
annotationProcessor 'com.github.OmerUygurOzer.knit:knitprocessor:v1.0.2'
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestCompile 'com.android.support.test:runner:1.0.1'
}
这正是Espresso中发生的事情。
Android JUnit runner在运行应用程序的同一进程中运行。如果您的应用程序中有运行时异常(即NullPointerException),它将破坏整个Instrumentation过程。
我手动运行应用程序时注意到了这一点。它运行良好,在运行仪器测试时,但在某些时候,仪表刚刚崩溃。
经过进一步调查后,我注意到在我们的应用程序中使用的模块中有一个NullPointerException。
如果你想避免仪器崩溃的情况,你可以使用Android Test Orchestrator(查看演示文稿here)。以这种方式运行测试,您将遇到孤立的崩溃。
如果您可以修复代码中发生的关键异常,那就去做吧:-)