Getting java.lang.RuntimeException: Stub when running Robolectric through Maven

android, intellij-idea, junit, maven, robolectric

Solution

Well, my problem was that I was importing from the wrong package:

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

I changed it to:

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

Now it's working as expected. I don't know why it was working in the IntelliJ.

Problem

Well, I'm having a strange bug. When I run my test through the IntelliJ, it passes without any problem. But if I run it using either the sure-fire plugin or the 'mvn clean test' command, I get the following exception: ``` shouldLoadMoreDataOnScrollBeyondTheThreshold(br.com.cybereagle.androidwidgets.listener.EndlessScrollListenerTest) Time elapsed: 2.73 sec <<< ERROR! java.lang.RuntimeException: Stub! at junit.framework.Assert.assertTrue(Assert.java:6) at br.com.cybereagle.androidwidgets.listener.EndlessScrollListenerTest.shouldLoadMoreDataOnScrollBeyondTheThreshold(EndlessScrollListenerTest.java:36) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:246) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:181) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103) at org.apache.maven.surefire.Surefire.run(Surefire.java:169) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350) at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021) ``` This is my test: ``` @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE) public class EndlessScrollListenerTest { private ListView listView; private EndlessScrollListener endlessScrollListener; @Before public void setUp() throws Exception { listView = mock(ListView.class); when(listView.getHeaderViewsCount()).thenReturn(0); when(listView.getFooterViewsCount()).thenReturn(0); endlessScrollListener = spy(new TestEndlessScrollListener(5)); } @Test public void shouldLoadMoreDataOnScrollBeyondTheThreshold() { doReturn(true).when(endlessScrollListener).hasMoreDataToLoad(); assertTrue(endlessScrollListener.isLoading()); endlessScrollListener.onScroll(listView, 2, 5, 20); assertFalse(endlessScrollListener.isLoading()); endlessScrollListener.onScroll(listView, 15, 5, 20); assertTrue(endlessScrollListener.isLoading()); endlessScrollListener.onScroll(listView, 21, 5, 40); assertFalse(endlessScrollListener.isLoading()); endlessScrollListener.onScroll(listView, 35, 5, 40); assertTrue(endlessScrollListener.isLoading()); endlessScrollListener.onScroll(listView, 38, 5, 60); assertFalse(endlessScrollListener.isLoading()); doReturn(false).when(endlessScrollListener).hasMoreDataToLoad(); endlessScrollListener.onScroll(listView, 55, 5, 60); assertFalse(endlessScrollListener.isLoading()); verify(endlessScrollListener, atMost(6)).hasMoreDataToLoad(); verify(endlessScrollListener, times(1)).loadMoreData(1); verify(endlessScrollListener, times(1)).loadMoreData(2); verify(endlessScrollListener, never()).loadMoreData(3); } private static class TestEndlessScrollListener extends EndlessScrollListener { private TestEndlessScrollListener(int visibleThreshold) { super(visibleThreshold); } @Override protected boolean hasMoreDataToLoad() { return false; } @Override protected void loadMoreData(int page) { } } } ``` The error happens in the first assertTrue: ``` assertTrue(endlessScrollListener.isLoading()); ``` The most strange thing is that according to the stacktrace, it happens inside the assertTrue method. These are my maven dependencies: ``` <dependencies> <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>${android.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.google.android</groupId> <artifactId>support-v4</artifactId> <version>${support.v4.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.android</groupId> <artifactId>support-v7-appcompat</artifactId> <version>${support.v7.version}</version> <scope>provided</scope> <type>jar</type> </dependency> <dependency> <groupId>org.robolectric</groupId> <artifactId>robolectric</artifactId> <version>${robolectric.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.2</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> ``` As I said, it works inside the IntelliJ. Another detail is that I have another project that generates a JAR that is using the Robolectric normally both from IntelliJ and from Maven. Any idea of what is happening?

Original source