-
-
Notifications
You must be signed in to change notification settings - Fork 380
Run unmockkAll
after each JUnit 5 test
#1297
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
Run unmockkAll
after each JUnit 5 test
#1297
Conversation
This also happens after each JUnit 4 test
ec167dd
to
92d1d0d
Compare
Note that gradle/actions/setup-gradle@v4 automatically does Gradle Wrapper validation, which is recommendable for open source repositories that accept contributions
a87c418
to
7314083
Compare
@@ -1,6 +1,6 @@ | |||
distributionBase=GRADLE_USER_HOME | |||
distributionPath=wrapper/dists | |||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip | |||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -4,11 +4,11 @@ import kotlinx.benchmark.gradle.benchmark | |||
|
|||
plugins { | |||
buildsrc.convention.`kotlin-jvm` | |||
id("org.jetbrains.kotlinx.benchmark") version "0.4.5" | |||
id("org.jetbrains.kotlinx.benchmark") version "0.4.12" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -26,7 +26,7 @@ benchmark { | |||
targets { | |||
register("main") { | |||
this as JvmBenchmarkTarget | |||
jmhVersion = "1.35" | |||
jmhVersion = "1.37" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome, thanks a lot! |
This PR originally aimed to fix #820. What it now does is:
clearAllMocks()
andunmockkAll()
, to demonstrate which of the two is more performant.MockKExtension
behave like the JUnit 4MockKRule
, making it callunmockkAll
after each unit test function. This PR makesMockKExtension
respect the JUnit 5 test instance lifecycle (either per method (default) or per class).MockKExtension
'sunmockkAll
behavior to theafterAll
callback #739. That PR assumed that nobody wants to unmock after each test. However, that should probably be the default practice to prevent leaking mocks, while allowing to deviate from it.Benchmark results (on my local machine)
Input:
$ ./gradlew benchmark
Output:
Conclusion
Contrary to what I originally claimed in #820,
unmockkAll
performs much faster (more operations per second) in the benchmarks in this PR, except when there's nothing to unmock (thenclearAllMocks
also is fast). This means that it's probably a good idea to unmock all after each unit test. This is whatMockKRule
does if applied as a JUnit 4@Rule
. However, before this PR the JUnit 5MockKExtension
doesn't callunmockkAll
after each test. This PR fixes that.In this way, #820 can now also be closed (after accepting these changes), since it is outdated / incorrect.