9

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?

2
  • 1
    just small comment about your tests - it is too many assertions per test. Does number of these scroll events or coordinates change a lot? If not I would leave only one. I also would split test into two - one for check that is loading if there is data, one of that it's not loading if there is no data. Commented Aug 27, 2013 at 17:11
  • Thanks for your tips. I'll try to improve this tests soon. Commented Aug 27, 2013 at 20:47

3 Answers 3

26

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Fernando, try the 'fest-assert' library for assertions. It is quite addictive and extends assertions possibility and readbility
I'm starting to use it. Actually, I'm using only the fest-android yet. I have to get used to using the fest. Thank you for remembering.
For JUnit5 this will be org.junit.jupiter.api.Assertions
5

That is silly but moving dependency of junit and put it before android dependencies will solve your issue. So your maven should be like this:

depndencies
-junit
-android

1 Comment

Thanks for your answer, but it's not my problem. I fixed it and I'll post the answer now.
0

Move the junit dependency up the chain on your referenced libraries and it will work. see here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.