问题
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