joecks
joecks

Reputation: 4637

How to run a Junit5 test with Android dependencies and robolectric

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?

Upvotes: 18

Views: 6736

Answers (2)

Genovich
Genovich

Reputation: 155

You can use junit-vintage-engine to run test with runners from JUnit4.

Just add org.junit.vintage:junit-vintage-engine to your dependencies, thus you can org.junit.runner.RunWith annotation to your tests.

Upvotes: 5

yuval
yuval

Reputation: 6428

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.

Upvotes: 4

Related Questions