Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 986
joecks Avatar asked Oct 27 '17 08:10

joecks


People also ask

Does JUnit 5 support Android?

A Gradle plugin that allows for the execution of JUnit 5 tests in Android environments using Android Gradle Plugin 4.0. 0 or later.

Is Robolectric deprecated?

setupActivity() is deprecated in Android unit test. Save this question.

What is Robolectric testing?

Robolectric is a framework that brings fast and reliable unit tests to Android. Tests run inside the JVM on your workstation in seconds. With Robolectric you can write tests like this: @RunWith(RobolectricTestRunner.


2 Answers

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.

like image 198
yuval Avatar answered Sep 16 '22 14:09

yuval


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.

like image 28
Genovich Avatar answered Sep 20 '22 14:09

Genovich