简体   繁体   English

Robolectric和Android Studio 1.1.0以及库测试

[英]Robolectric and Android Studio 1.1.0 and library testing

I am having trouble getting my Robolectric unit tests to run under the experimental AS 1.1 unit testing variant. 我无法让我的Robolectric单元测试在实验性AS 1.1单元测试变体下运行。 The error I am getting is shown below; 我得到的错误如下所示;

java.lang.NoClassDefFoundError: com/my/app/R$string
    at com.my.app.MoneyFormatter.formatDealMessage(MoneyFormatter.java:63)
    ...
Caused by: java.lang.ClassNotFoundException: com.my.app.R$string
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at org.robolectric.bytecode.AsmInstrumentingClassLoader.loadClass(AsmInstrumentingClassLoader.java:100)
    ... 46 more

I retrieve this error by executing from the command line at the root (app-name in Snippet1 ) the command; 我通过执行从命令行在root( snippet1中的 app-name)命令执行此错误;

./gradlew core:library:test

This approach works in Android Studio 1.1 without unit testing enabled and using the android gradle tooling at v1.0 (com.android.tools.build:gradle:1.0.0). 这种方法适用于Android Studio 1.1,无需启用单元测试,并在v1.0上使用android gradle工具(com.android.tools.build:gradle:1.0.0)。

Project configuration 项目配置

My project has a complex structure as prescribed below; 我的项目具有如下规定的复杂结构;

app-name
 |
 |->app
    |->src
       |-> androidTest // Espresso helper classes reside here
       |-> androidTestFlavour1 // Espresso int tests relating to 'flavour 1' 
       |-> androidTestFlavour2 // Espresso int tests relating to 'flavour 2'
       |-> flavour1
       |-> flavour2
       |-> main
           + AndroidManifest.xml // Manifest for apps
       |-> testFlavour1 // Robolectric unit tests for flavour 1
       |-> testFlavour2 // Robolectric unit tests for flavour 2
 |->core
       |->library
           |-> src
              |-> main // Library code resides here
                  + AndroidManifest.xml
              |-> test // Robolectric unit tests for library
 +gradle
 +gradlew
 +settings.gradle
 +local.properties

Snippet 1 : Project schematic 代码段1 :项目原理图

I have followed various pieces of advice from Pivotal Labs themselves and numerous other home-brew alternatives with no success. 我遵循了Pivotal Labs自己的各种建议和许多其他家庭酿造替代品,但没有成功。

What is the error saying and what do I need to change to put it right? 什么是错误说明,我需要改变什么才能正确?

Update: So having inspected the classpath being used to run the tests I noted the following library paths; 更新:所以检查了用于运行测试的类路径后,我注意到以下库路径;

  • /workspace/app/core/lib/build/intermediates/bundles/debug/classes.jar /workspace/app/core/lib/build/intermediates/bundles/debug/classes.jar
  • /workspace/app/core/lib/build/intermediates/classes/test/debug /工作区/应用程序/核心/ LIB /建立/中间体/类/测试/调试
  • /workspace/app/core/lib/build/intermediates/dependency-cache/test/debug /工作区/应用程序/核心/ LIB /构建/中间体/依赖性缓存/测试/调试

Neither of these directories contain the generated R$string.class file hence the exception. 这些目录都不包含生成的R$string.class文件,因此异常。 The directory containing the generated 'R' files include the debug and release build types of the application. 包含生成的“R”文件的目录包括应用程序的调试和发布版本类型。 So; 所以;

  • /workspace/app/core/lib/build/intermediates/classes/debug /工作区/应用程序/核心/ LIB /建立/中间体/类/调试
  • /workspace/app/core/lib/build/intermediates/classes/release /工作区/应用程序/核心/ LIB /建立/中间体/类/释放

This indicates that a part of the build process is missing adding the "debug" or "release" build type resources. 这表明添加“调试”或“发布”构建类型资源时缺少构建过程的一部分。 This also backs-up the behaviour whereby pure-Java test cases (that only rely on the classes.jar) work fine. 这也支持纯Java测试用例(仅依赖于classes.jar)的行为。

OK! 好! I've got it working. 我有它的工作。 I have added the following line to the library build gradle to forcibly add the generated "R" files onto the test classpath; 我已将以下行添加到库构建gradle中,以强制将生成的“R”文件添加到测试类路径中;

sourceSets {
  test.java.srcDirs += "build/generated/source/r/debug"
}

With this I have reached the nirvana of Robolectric tests with; 有了这个,我已经达到了Robolectric测试的必杀技;

./gradlew core:lib:test

and Espresso 2.0 -based integration with; 和基于Espresso 2.0的集成;

./gradlew connectedAndroidTest

Stop the clock for unit and integration test support in the IDE.... oh, what's that? 在IDE中停止时钟以获得单元和集成测试支持....哦,那是什么? 7 YEARS?! 7年?! No love lost. 爱未遗失。 Kudos to this answer that showed me how to forcibly add stuff to the test classpath. 感谢这个答案向我展示了如何强行将东西添加到测试类路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM