9

好吧,我遇到了一个奇怪的错误。当我通过 IntelliJ 运行测试时,它毫无问题地通过了。但是,如果我使用 sure-fire 插件或“mvn clean test”命令运行它,我会得到以下异常:

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)

这是我的测试:

@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) {

        }
    }
}

错误发生在第一个 assertTrue 中:

assertTrue(endlessScrollListener.isLoading());

最奇怪的是,根据堆栈跟踪,它发生在 assertTrue 方法内部。

这些是我的 Maven 依赖项:

<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>

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

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

知道发生了什么吗?

4

3 回答 3

26

好吧,我的问题是我从错误的包中导入:

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

我将其更改为:

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

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

于 2013-08-27T13:24:57.847 回答
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
于 2013-08-27T11:09:11.113 回答
0

将 junit 依赖项向上移动到您引用的库上,它将起作用。看这里

于 2015-07-20T12:56:50.907 回答