android – 获取java.lang.RuntimeException:通过Maven运行Robolectric时存根

前端之家收集整理的这篇文章主要介绍了android – 获取java.lang.RuntimeException:通过Maven运行Robolectric时存根前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好吧,我有一个奇怪的错误.当我通过IntelliJ运行我的测试时,它没有任何问题.但是如果我使用sure-fire插件或’mvn clean test’命令运行它,我会得到以下异常:
  1. shouldLoadMoreDataOnScrollBeyondTheThreshold(br.com.cybereagle.androidwidgets.listener.EndlessScrollListenerTest) Time elapsed: 2.73 sec <<< ERROR!
  2. java.lang.RuntimeException: Stub!
  3. at junit.framework.Assert.assertTrue(Assert.java:6)
  4. at br.com.cybereagle.androidwidgets.listener.EndlessScrollListenerTest.shouldLoadMoreDataOnScrollBeyondTheThreshold(EndlessScrollListenerTest.java:36)
  5. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  6. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  7. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  8. at java.lang.reflect.Method.invoke(Method.java:616)
  9. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
  10. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  11. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
  12. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
  13. at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
  14. at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:246)
  15. at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
  16. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
  17. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
  18. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
  19. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
  20. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  21. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
  22. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
  23. at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:181)
  24. at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
  25. at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
  26. at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120)
  27. at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103)
  28. at org.apache.maven.surefire.Surefire.run(Surefire.java:169)
  29. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  30. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  31. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  32. at java.lang.reflect.Method.invoke(Method.java:616)
  33. at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
  34. at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)

这是我的测试:

  1. @RunWith(RobolectricTestRunner.class)
  2. @Config(manifest = Config.NONE)
  3. public class EndlessScrollListenerTest {
  4.  
  5. private ListView listView;
  6. private EndlessScrollListener endlessScrollListener;
  7.  
  8. @Before
  9. public void setUp() throws Exception {
  10. listView = mock(ListView.class);
  11.  
  12. when(listView.getHeaderViewsCount()).thenReturn(0);
  13. when(listView.getFooterViewsCount()).thenReturn(0);
  14.  
  15.  
  16. endlessScrollListener = spy(new TestEndlessScrollListener(5));
  17. }
  18.  
  19. @Test
  20. public void shouldLoadMoreDataOnScrollBeyondTheThreshold() {
  21. doReturn(true).when(endlessScrollListener).hasMoreDataToLoad();
  22.  
  23. assertTrue(endlessScrollListener.isLoading());
  24.  
  25. endlessScrollListener.onScroll(listView,2,5,20);
  26.  
  27. assertFalse(endlessScrollListener.isLoading());
  28.  
  29. endlessScrollListener.onScroll(listView,15,20);
  30.  
  31. assertTrue(endlessScrollListener.isLoading());
  32.  
  33.  
  34. endlessScrollListener.onScroll(listView,21,40);
  35.  
  36. assertFalse(endlessScrollListener.isLoading());
  37.  
  38. endlessScrollListener.onScroll(listView,35,40);
  39.  
  40. assertTrue(endlessScrollListener.isLoading());
  41.  
  42. endlessScrollListener.onScroll(listView,38,60);
  43.  
  44. assertFalse(endlessScrollListener.isLoading());
  45.  
  46. doReturn(false).when(endlessScrollListener).hasMoreDataToLoad();
  47.  
  48. endlessScrollListener.onScroll(listView,55,60);
  49.  
  50. assertFalse(endlessScrollListener.isLoading());
  51.  
  52.  
  53. verify(endlessScrollListener,atMost(6)).hasMoreDataToLoad();
  54. verify(endlessScrollListener,times(1)).loadMoreData(1);
  55. verify(endlessScrollListener,times(1)).loadMoreData(2);
  56. verify(endlessScrollListener,never()).loadMoreData(3);
  57. }
  58.  
  59. private static class TestEndlessScrollListener extends EndlessScrollListener {
  60.  
  61. private TestEndlessScrollListener(int visibleThreshold) {
  62. super(visibleThreshold);
  63. }
  64.  
  65. @Override
  66. protected boolean hasMoreDataToLoad() {
  67. return false;
  68. }
  69.  
  70. @Override
  71. protected void loadMoreData(int page) {
  72.  
  73. }
  74. }
  75. }

错误发生在第一个assertTrue中:

  1. assertTrue(endlessScrollListener.isLoading());

最奇怪的是,根据stacktrace,它发生在assertTrue方法中.

这些是我的maven依赖项:

  1. <dependencies>
  2. <dependency>
  3. <groupId>android</groupId>
  4. <artifactId>android</artifactId>
  5. <version>${android.version}</version>
  6. <scope>provided</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>com.google.android</groupId>
  10. <artifactId>support-v4</artifactId>
  11. <version>${support.v4.version}</version>
  12. <scope>compile</scope>
  13. </dependency>
  14. <dependency>
  15. <groupId>com.google.android</groupId>
  16. <artifactId>support-v7-appcompat</artifactId>
  17. <version>${support.v7.version}</version>
  18. <scope>provided</scope>
  19. <type>jar</type>
  20. </dependency>
  21.  
  22. <dependency>
  23. <groupId>org.robolectric</groupId>
  24. <artifactId>robolectric</artifactId>
  25. <version>${robolectric.version}</version>
  26. <scope>test</scope>
  27. </dependency>
  28.  
  29. <dependency>
  30. <groupId>org.hamcrest</groupId>
  31. <artifactId>hamcrest-core</artifactId>
  32. <version>1.2</version>
  33. <scope>test</scope>
  34. </dependency>
  35.  
  36. <dependency>
  37. <groupId>junit</groupId>
  38. <artifactId>junit</artifactId>
  39. <version>4.8.2</version>
  40. <scope>test</scope>
  41. </dependency>
  42. </dependencies>

正如我所说,它在IntelliJ内部工作.

另一个细节是我有另一个项目生成一个JAR,通常使用来自IntelliJ和Maven的Robolectric.

知道发生了什么事吗?

解决方法

好吧,我的问题是我从错误的包导入:
  1. import static junit.framework.Assert.assertFalse;
  2. import static junit.framework.Assert.assertTrue;

我改成了:

  1. import static org.junit.Assert.assertFalse;
  2. import static org.junit.Assert.assertTrue;

现在它按预期工作了.我不知道为什么它在IntelliJ中工作.

猜你在找的Android相关文章