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

Added a JUnit 4 rule #816

Merged
merged 6 commits into from May 4, 2022
Merged

Added a JUnit 4 rule #816

merged 6 commits into from May 4, 2022

Conversation

jonapoul
Copy link
Contributor

@jonapoul jonapoul commented May 2, 2022

I find myself using something like this in most of my personal projects, it saves a lot (okay not that much) boilerplate of calling MockKAnnotations.init(this) and unmockkAll() in every JUnit 4 test case.

I essentially copypasted the test case from the equivalent for MockKExtension, let me know if that's not okay.

@Raibaz
Copy link
Collaborator

Raibaz commented May 3, 2022

This is pretty cool, thanks a lot for submitting it!

Would you mind also updating the docs here to mention that this can be done with JUnit 4 as well?

Thanks again!

@jonapoul
Copy link
Contributor Author

jonapoul commented May 3, 2022

Added a bit next to the JUnit5 section 😃

@Raibaz
Copy link
Collaborator

Raibaz commented May 3, 2022

Beautiful, thanks a lot!

...I guess now the only thing left to do is figuring out why the tests are failing :)

@Raibaz Raibaz mentioned this pull request May 3, 2022
@Raibaz
Copy link
Collaborator

Raibaz commented May 3, 2022

I've been digging a bit into this, and looks like parameter injection is not easy to achieve with JUnit 4.

Would you be ok removing injectsValidMockInMethods and injectsValidRelaxedMockInMethods and just having MockKRuleTest perform MockkAnnotations.init(this) and unmockkAll()()

@jonapoul
Copy link
Contributor Author

jonapoul commented May 3, 2022

Agree, hopefully that should sort it.

Copy link
Collaborator

@Raibaz Raibaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the build to pass on my machine, but I can't push on your branch :)

import io.mockk.impl.annotations.SpyK
import io.mockk.verify
import org.junit.Rule
import org.junit.jupiter.api.Test
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should actually be org.junit.Test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed!

import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need RunWith(JUnit4::class) here for the tests to pass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@Raibaz
Copy link
Collaborator

Raibaz commented May 4, 2022

Thanks a lot for following up on this!

@Raibaz Raibaz merged commit a1ed737 into mockk:master May 4, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants