How to run a Junit5 test with Android dependencies and robolectric

笑着哭i 提交于 2021-02-07 13:15:45

问题


I am trying to run a unit test like that:

    @org.junit.jupiter.api.Test
    void junit5codeCoverage() {
        final int result = new Foo().junit5();

        Assert.assertEquals(Looper.getMainLooper().getThread(), Thread.currentThread());

        assertEquals(-1, result);
    }

That is a Junit5 test with Android dependencies (i.e Looper.getMainLooper()) with Robolectric.

I am using the junit5 android plugin from mannodermaus that allows running junit5 within Android setups. But this does not work out of the box because it would not load robolectric. So I tried alixwar's branch that would tackle robolectric and junit5 test coverage, but still, would not use Android classes.

I furthermore started to investigate how to run a robolectric test on junit5, which would require understanding how the RobolectricTestRunner works and how to port the code to the JUnit5 platform. I have little understanding of the new junit5 platform, but I figured out that I could build on top of the org.junit.platform.runner.JUnitPlatform runner, to follow the test runner concept, which is part of the junit-platform-runner package. But this is so far away from the original Junit4 SandBoxTestRunner that I couldn't manage to complete the port.

So what would be the most feasible path to implement robolectric junit5 support, or is there any (obvious) concept I am missing?


回答1:


I am trying to do this too, but it seems that at the time of this writing, Robolectric simply does not support junit5.

See the discussion here: https://github.com/robolectric/robolectric/issues/2996

Some workarounds are described in that discussion, but for now I am just going to stick to junit4.



来源:https://stackoverflow.com/questions/46971063/how-to-run-a-junit5-test-with-android-dependencies-and-robolectric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!