How to run a Junit5 test with Android dependencies and robolectric
Asked Answered
P

2

18

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?

Prone answered 27/10, 2017 at 8:50 Comment(0)
S
5

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.

Sharper answered 12/7, 2021 at 8:31 Comment(2)
More details about the above answer: What this means is that you'll need to use JUnit4 @Test for tests that use @RunWith(RobolectricTestRunner::class). And I suggest removing your JUnit 4 dependency from the gradle build: testImplementation 'junit:junit:4.+' // should get rid of this old JUnit.Siple
Big picture is: let's encourage Roboelectric to move to Jupiter so we can drop these special cases.Siple
A
4

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.

Aguste answered 27/7, 2018 at 23:46 Comment(1)
2024: still waiting...Stretto

© 2022 - 2024 — McMap. All rights reserved.