android annotations,gradle和robolectric:java.lang.NoClassDefFoundError:org / androidannotations / api / view / HasViews

时间:2014-06-11 12:03:58

标签: android unit-testing testing robolectric android-annotations

我成功地让Robolectric在一个gradle项目中工作。现在我在使用Android Annotations时遇到问题。因此,我已经加入了测试包和主程序包(Android Annotations的Generated类只有包可见性)。我在https://github.com/excilys/androidannotations/wiki/Unit-test-your-application阅读了有关单元测试的部分。

任何与活动无关的测试都有效,例如:

@Test
public void testTrueIsTrue() throws Exception {
    Assertions.assertThat(true);
}

现在的问题是,当尝试执行有关活动及其生命周期的测试时,测试会因NoClassDefFoundError而失败。您是否知道如何测试活动?关于此的测试是例如:

@Test
public void testActivity() {
    MainActivity_ mainActivity = new MainActivity_();
    mainActivity.onCreate(null);
    Assert.assertNotNull(mainActivity);
}

@Test
public void testRobolectricActivity() {
    ActionBarActivity activity = Robolectric.buildActivity(MainActivity_.class).create().get();
    Assert.assertNotNull(activity);
}

最后一次测试的结果错误及其堆栈跟踪是:

java.lang.NoClassDefFoundError: path/to/project/app/ui/MainActivity_
at path.to.project.test.MainActivityRobolectricTest.testRobolectricActivity(MainActivityRobolectricTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:250)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

第一次测试

java.lang.NoClassDefFoundError: org/androidannotations/api/view/HasViews
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
at org.robolectric.bytecode.AsmInstrumentingClassLoader.findClass(AsmInstrumentingClassLoader.java:165)
at org.robolectric.bytecode.AsmInstrumentingClassLoader.loadClass(AsmInstrumentingClassLoader.java:98)
at path.to.projet.test.MainActivityRobolectricTest.testActivity(MainActivityRobolectricTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:250)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: org.androidannotations.api.view.HasViews
at org.robolectric.bytecode.AsmInstrumentingClassLoader.findClass(AsmInstrumentingClassLoader.java:131)
at org.robolectric.bytecode.AsmInstrumentingClassLoader.loadClass(AsmInstrumentingClassLoader.java:98)... 43 more

我很感激一些帮助。提前谢谢。

3 个答案:

答案 0 :(得分:0)

我还没有使用过roboelectric,但听起来AA要么没有产生类别。

在apt_generated文件夹中查找MainAcitvity_,检查文件是否存在,并且它具有hasviews方法。

其次,这会对非生成类

产生影响
@Test
public void testActivity() {
    MainActivity mainActivity = new MainActivity_();
    mainActivity.onCreate(null);
    Assert.assertNotNull(mainActivity);
}

答案 1 :(得分:0)

我有类似的问题。问题是由于错误仍然找不到生成的类。 而且我看到你也在使用SNAPSHOT版本,我也有。

我不确定,但在我的情况下,Android Annotations没有告诉我在我的带注释的代码中有错误,但我检查了最近编码的方法,并意识到我用@Background注释的一个方法具有私有访问权限。这是问题所在。在制作包装之后,一切都恢复了。

另一个问题可能是您没有在tests vm选项中正确指定构建目录。因此,您必须执行以下操作:

将编辑配置下的vm选项重置为-ea(默认值)。 之后你会得到一个STUB!执行测试时出现异常,它抱怨junit版本。将异常控制台顶部的所有内容(它稍微灰色,你必须打开它)复制到编辑器中,删除-classpath之前的所有内容以及不属于类路径的所有内容(意味着不用以下分隔:和追加构建目录的最后一个路径:

在我的案例中看起来像这样:

-classpath /home/user/.gradle/caches/modules-2/files-2.1/junit/junit/4.12-SNAPSHOT/d0f5ec9e335da2ea2827c781a46a02d8b6bbd556/junit-4.12-SNAPSHOT.jar:/home/user/android-studio/lib/idea_rt.jar:/home/user/android-studio/plugins/junit/lib/junit-rt.jar:/home/user/android-sdk/platforms/android-19/android.jar:/home/user/android-sdk/platforms/android-19/data/res:/home/user/android-sdk/tools/support/annotations.jar:   [... deleted some dependencies here ...]   /home/user/.gradle/caches/modules-2/files-2.1/org.robolectric/robolectric/2.4-SNAPSHOT/78161192eaadd6c7c56f6cf48040fa46897163d0/robolectric-2.4-SNAPSHOT.jar:/home/user/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-util/5.0.1/7c8caddfbd0b2d7b844f8fcc75175b9cb9cf4724/asm-util-5.0.1.jar:/home/user/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.3-SNAPSHOT/90850100a41d5695e2780773e2fc33198dea70d/gson-2.3-SNAPSHOT.jar:/home/user/development/App/app/build/generated

之后应找到课程。

我希望这会有所帮助......

答案 2 :(得分:0)

如果没有看到您的项目设置,我不明白您的问题。我能告诉你robolectric和android注释一起工作。这是我的示例应用模板,其中包含活动测试https://github.com/nenick/android-gradle-template