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

Create Robolectric Extension #2

Closed
LeoColman opened this issue May 13, 2023 · 8 comments
Closed

Create Robolectric Extension #2

LeoColman opened this issue May 13, 2023 · 8 comments
Labels
enhancement New feature or request

Comments

@LeoColman
Copy link
Owner

Related: kotest/kotest#3505
Archived Kotest repository: https://github.com/kotest/kotest-extensions-robolectric

@LeoColman LeoColman added the enhancement New feature or request label May 13, 2023
@jkasten2
Copy link

@LeoColman This would be really nice to have! I am a user of both Kotest and Robolectric and trying to find any solutions to get them to work together in the same test class.

Attempted workarounds

I attempt to use kotest-robolectric-extension (was official but no longer supported) and applied a "patch" to get it working but its not fully working: kotest/kotest#2717 (comment)

kotest-android with Robolectric

Back to the topic here, what do you think it would take to get this plugin working with Robolectric? Interested on what you think has to be done, as it's something I am interested attempting, but looking for help.

@LeoColman
Copy link
Owner Author

LeoColman commented Feb 16, 2024

There is one specific class in Robolectric that is very hard to understand and plug into.

I'm the original author of the Robolectric extension, and it took me a lot of time to understand the main driver from Robolectric, RobolectricTestRunner. After I managed to put something up, it changed and broke the extension and I've never had the capacity to give it another try.


Once you are able to get inside Robolectric's environment, Kotest works as expected.

https://github.com/kotest/kotest-extensions-robolectric/blob/9b5779d0881d56af5da30661ddf7272e06bcc7d7/extensions-robolectric/src/main/kotlin/io/kotest/extensions/robolectric/RobolectricExtension.kt#L43

The idea is that the Spec must be wrapped around to whatever it is that Robolectric uses to create classes

@jkasten2
Copy link

@LeoColman Thanks for the help here! I looked through RobolectricTestRunner and Robolectric's SandboxTestRunner and found a key difference. kotest-robolectric-extension was not calling runOnMainThread like Robolectric's SandboxTestRunner was. Adding that got me up and running and put extra details and the exact code in this comment kotest/kotest#2717 (comment).

@LeoColman
Copy link
Owner Author

Cool @jkasten2!
Whenever I have the chance I'll publish an artifact with the fixed version!

@LeoColman
Copy link
Owner Author

@jkasten2 Could you share the full code of the extension you wrote? I'm having a bit of a hard time trying to change from the old model as it was Spec-based, not test based

@jkasten2
Copy link

jkasten2 commented Mar 1, 2024

@LeoColman here is the full set of source I have been using:
https://github.com/jkasten2/patches-to-kotest-robolectric-extension/tree/master/patched-kotest-robolectric-extension/src/main/java/com/example/patched_kotest_robolectric_extension

The repo includes a full Android project, and you can go back a commit to see the one of the test that fails, then pass later after the changes:
https://github.com/jkasten2/patches-to-kotest-robolectric-extension/tree/master

The changes were much more than I thought originally, I took kotest-robolectric-extension/master and copied my full set of changes over the top in this commit:
jkasten2/patches-to-kotest-robolectric-extension@7c9c870

Hopefully this is helpful, but let me know if you need any more detail, or want to to hunt down the differences a bit more.

@LeoColman
Copy link
Owner Author

Thanks a lot @jkasten2 !

Closing this issue as released on 7c1a520

@jkasten2
Copy link

jkasten2 commented Mar 7, 2024

@LeoColman Awesome!, this is very helpful so we don't have to keep running with a patched copy locally. Also removes complexity of our project's tests and we can get patches from the community easier now!

I tested out br.com.colman:kotest-extensions-android:0.1.1 with org.robolectric:robolectric:4.8.1 and io.kotest:kotest-runner-junit5:5.8.0, it's all working together for me.

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

No branches or pull requests

2 participants