Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to run junit5 tests with robolectric? #305

Closed
illuzor opened this issue Sep 11, 2023 · 5 comments
Closed

Is it possible to run junit5 tests with robolectric? #305

illuzor opened this issue Sep 11, 2023 · 5 comments

Comments

@illuzor
Copy link

illuzor commented Sep 11, 2023

I see that RobolectricTestRunner is based in junit4 runner and i have initialization error
image

@mannodermaus
Copy link
Owner

mannodermaus commented Sep 12, 2023

The short answer is no.

Warning
This ↑ is not entirely accurate anymore – scroll down to find out more.

The way Robolectric injects the modified version of the android.jar file for tests depends heavily on custom class loading via their custom Runner, which cannot be used from JUnit 5 (in other words, you can't mix Jupiter's @Test with JUnit 4's @RunWith).

That being said, a recent version upgrade of JUnit 5 has introduced the ability for custom class loading. If you want to dive into the lengthy discussion, the ticket is here. With this merged, it's theoretically possible to port over Robolectric's old code to a JUnit 5 extension – the main problem is, nobody was able to dedicate time to it just yet.

The Robolectric feature ticket to support JUnit 5 is here and I'm crossing my fingers that we will see a Robolectric extension in the future! Until then, please stick to @org.junit.Test for your Robolectric tests. 🙇‍♂️

(I'm going to pin this issue for visibility since this question does come up every once in a while. Hope you don't mind!)

@mannodermaus mannodermaus pinned this issue Sep 12, 2023
@illuzor
Copy link
Author

illuzor commented Sep 12, 2023

@mannodermaus thank you for the answer

@emartynov
Copy link

People, is it possible to run Robolectric with vintage engine?

@mannodermaus
Copy link
Owner

@emartynov It is! If you annotate your Robolectric tests with the JUnit 4 annotations (i.e. @org.junit.Test and friends) and run them through the vintage engine, it'll work just fine. This is why I generally recommend people to prefer JUnit 5, write as many tests as possible with that API, and only resort to JUnit 4 for tests that do require Robolectric and its custom runner.

@mannodermaus
Copy link
Owner

Some new developments are spinning up in this space!

First, @warnyul the absolute GOAT has started publicizing an interop plugin to bridge the gap between Robolectric and JUnit 5. Essentially, it is possible to run those tests on the JUnit Platform now! YMMV, but my first impression is very good. Show him some love by starring it!

Second, Robolectric is participating in Google Summer of Code 2024. One of the suggested projects in their pitch deck deals with the refactoring of Robolectric to support JUnit 5! Of course, this is a much more long-term undertaking, but I'm delighted to see that they are starting to throw the idea of evolution out there. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants