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

前端之家收集整理的这篇文章主要介绍了android – 获取java.lang.RuntimeException:通过Maven运行Robolectric时存根前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好吧,我有一个奇怪的错误.当我通过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,20);

        assertTrue(endlessScrollListener.isLoading());


        endlessScrollListener.onScroll(listView,21,40);

        assertFalse(endlessScrollListener.isLoading());

        endlessScrollListener.onScroll(listView,35,40);

        assertTrue(endlessScrollListener.isLoading());

        endlessScrollListener.onScroll(listView,38,60);

        assertFalse(endlessScrollListener.isLoading());

        doReturn(false).when(endlessScrollListener).hasMoreDataToLoad();

        endlessScrollListener.onScroll(listView,55,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());

最奇怪的是,根据stacktrace,它发生在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,通常使用来自IntelliJ和Maven的Robolectric.

知道发生了什么事吗?

解决方法

好吧,我的问题是我从错误的包导入:
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中工作.

原文链接:https://www.f2er.com/android/318121.html

猜你在找的Android相关文章