Android单元测试遇到的问题

使用的框架:Robolectric 3.0 or Espresso


1. FileNotFoundException: build\intermediates\bundles\debug\AndroidManifest.xml (系统找不到指定的路径。)

解决方法:编辑configuration,设置工作目录到对应的module即可


2. 错误: 无法访问AndroidHttpClient
找不到android.net.http.AndroidHttpClient的类文件


解决方法:从sdk\platforms\android-23\optional目录下拷贝org.apache.http.legacy.jar到工程中并加入依赖中即可


3. java.lang.RuntimeException: Method v in android.util.Log not mocked

解决方法:

testOptions {
    unitTests.returnDefaultValues = true
}


4. FileNotFoundException: build\intermediates\assets\debug\ap2d.data

通常只有测试模块的assets资源会在编译时放在build\intermediates\assets\debug,其他的如包含在jar或aar文件中的一般不会放在该目录,那么roboletric在查找文件时就找不到

解决方法:从其他地方拷贝到该目录即可,还有就是重写RobolectricGradleTestRunner.getAppManifest方法返回新的assetDir


5. 错误:IllegalArgumentException: input == null!
at javax.imageio.ImageIO.createImageInputStream(ImageIO.java:339)
at org.robolectric.shadows.ShadowBitmapFactory.getImageSizeFromStream(ShadowBitmapFactory.java:223)
at org.robolectric.shadows.ShadowBitmapFactory.decodeStream(ShadowBitmapFactory.java:104)
at org.robolectric.shadows.ShadowBitmapFactory.decodeStream(ShadowBitmapFactory.java:98)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java)

通常是由于传递给decodeStream的InputStream参数是由Resources.openRawResource返回的,且为空,为空是因为Resources对象未shadowed,故取不到资源


解决方法:使用BitmapFactory.decodeResource来加载图片


6. 测试时需要用到的公共类,放到一个module里,在需要引用的module中添加testCompile


7. 测试RxJava Observable时可以设置SubscribeOnScheduler和ObserverOnScheduler为Schedulers.immediate(),使一切同步进行,避免异步执行在不同线程可能导致的问题


8. 在测试使用OkHttp的网络调用时可以设置Intercepter来拦截请求,返回预设的值


9. 使用Expresso进行点击测试报如下错误:

android.support.test.espresso.PerformException: Error performing 'single click' on view 'with string from resource id: <2131165384>[no_account_instant_register] value: 没有账号,立即注册'.
...
Caused by: android.support.test.espresso.PerformException: Error performing 'Send down montion event' on view 'unknown'.
...
Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
...
Caused by: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
...

通常是由于键盘等遮住了目标View

详情见https://code.google.com/p/android/issues/detail?id=190169

解决方法:可能是关闭输入法


10. 在Espresso中读取文件数据

方法:

a. 在AndroidTest目录下建立以assets目录,将文件放到其中,该目录中的文件会被打包到测试apk中;

b. 调用InstrumentationRegistry.getContext().getAssets()获得AssetsManager,再通过AssetsManager来打开文件


11. 使用Espresso进行测试时,如果有异步操作,可以使用IdlingResource等待异步操作结束

参考见https://www.future-processing.pl/blog/android-testing-with-espresso-2-and-dagger-2-mocking-long-running-operations/


12. 改变Robolectric的版本到3.1遇到的问题

Downloading: org/robolectric/shadows-core/3.1/shadows-core-3.1.pom from repository sonatype at https://oss.sonatype.org/content/groups/public/
Error transferring file: oss.sonatype.org
[WARNING] Unable to get resource 'org.robolectric:shadows-core:pom:3.1' from repository sonatype (https://oss.sonatype.org/content/groups/public/): Error transferring file: oss.sonatype.org
Downloading: org/robolectric/shadows-core/3.1/shadows-core-3.1.pom from repository central at http://repo1.maven.org/maven2
Error transferring file: repo1.maven.org
[WARNING] Unable to get resource 'org.robolectric:shadows-core:pom:3.1' from repository central (http://repo1.maven.org/maven2): Error transferring file: repo1.maven.org
Downloading: org/robolectric/shadows-core/3.1/shadows-core-3.1-21.jar from repository sonatype at https://oss.sonatype.org/content/groups/public/
Error transferring file: oss.sonatype.org
Downloading: org/robolectric/shadows-core/3.1/shadows-core-3.1-21.jar from repository central at http://repo1.maven.org/maven2
Error transferring file: repo1.maven.org
[WARNING] Unable to get resource 'org.robolectric:shadows-core:jar:21:3.1' from repository sonatype (https://oss.sonatype.org/content/groups/public/): Error transferring file: oss.sonatype.org
[WARNING] Unable to get resource 'org.robolectric:shadows-core:jar:21:3.1' from repository central (http://repo1.maven.org/maven2): Error transferring file: repo1.maven.org

Unable to resolve artifact: Missing:
----------
1) org.robolectric:shadows-core:jar:21:3.1

  Try downloading the file manually from the project website.

  Then, install it using the command: 
      mvn install:install-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.1 -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there: 
      mvn deploy:deploy-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.1 -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency: 
  1) org.apache.maven:super-pom:pom:2.0
  2) org.robolectric:shadows-core:jar:21:3.1

----------
1 required artifact is missing.

for artifact: 
  org.apache.maven:super-pom:pom:2.0

from the specified remote repositories:
  central (http://repo1.maven.org/maven2),
  sonatype (https://oss.sonatype.org/content/groups/public/)

at org.apache.maven.artifact.ant.DependenciesTask.doExecuteResolution(DependenciesTask.java:268)
at org.apache.maven.artifact.ant.DependenciesTask.doExecute(DependenciesTask.java:168)
at org.apache.maven.artifact.ant.AbstractArtifactTask.execute(AbstractArtifactTask.java:751)
at org.robolectric.internal.dependency.MavenDependencyResolver.getLocalArtifactUrls(MavenDependencyResolver.java:52)
at org.robolectric.internal.dependency.CachedDependencyResolver.getLocalArtifactUrls(CachedDependencyResolver.java:43)
at org.robolectric.internal.InstrumentingClassLoaderFactory.getSdkEnvironment(InstrumentingClassLoaderFactory.java:43)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:190)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:56)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:157)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException: Missing:
----------
1) org.robolectric:shadows-core:jar:21:3.1

  Try downloading the file manually from the project website.

  Then, install it using the command: 
      mvn install:install-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.1 -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there: 
      mvn deploy:deploy-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.1 -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency: 
  1) org.apache.maven:super-pom:pom:2.0
  2) org.robolectric:shadows-core:jar:21:3.1

----------
1 required artifact is missing.

for artifact: 
  org.apache.maven:super-pom:pom:2.0

from the specified remote repositories:
  central (http://repo1.maven.org/maven2),
  sonatype (https://oss.sonatype.org/content/groups/public/)

at org.apache.maven.artifact.resolver.DefaultArtifactResolver.resolveTransitively(DefaultArtifactResolver.java:360)
at org.apache.maven.artifact.ant.DependenciesTask.doExecuteResolution(DependenciesTask.java:263)
... 23 more

原因:网络问题

解决方法:多次尝试,或找一个好的VPN

你可能感兴趣的:(Android单元测试遇到的问题)