Repository navigation
Run unmockkAll after each JUnit 5 test - #1297
Merged
Merged
Conversation
This also happens after each JUnit 4 test
erikhuizinga
force-pushed
the
erikhuizinga-bench-junit-api
branch
from
September 17, 2024 13:20
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
erikhuizinga
force-pushed
the
erikhuizinga-bench-junit-api
branch
from
September 17, 2024 13:28
a87c418 to
7314083
Compare
erikhuizinga
marked this pull request as ready for review
September 17, 2024 14:48
erikhuizinga
commented
Sep 17, 2024
| 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 |
Contributor
Author
There was a problem hiding this comment.
erikhuizinga
commented
Sep 17, 2024
| plugins { | ||
| buildsrc.convention.`kotlin-jvm` | ||
| id("org.jetbrains.kotlinx.benchmark") version "0.4.5" | ||
| id("org.jetbrains.kotlinx.benchmark") version "0.4.12" |
Contributor
Author
There was a problem hiding this comment.
| register("main") { | ||
| this as JvmBenchmarkTarget | ||
| jmhVersion = "1.35" | ||
| jmhVersion = "1.37" |
Contributor
Author
There was a problem hiding this comment.
Collaborator
|
This is awesome, thanks a lot! |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR originally aimed to fix #820. What it now does is:
clearAllMocks()andunmockkAll(), to demonstrate which of the two is more performant.MockKExtensionbehave like the JUnit 4MockKRule, making it callunmockkAllafter each unit test function. This PR makesMockKExtensionrespect the JUnit 5 test instance lifecycle (either per method (default) or per class).MockKExtension'sunmockkAllbehavior to theafterAllcallback #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 benchmarkOutput:
Conclusion
Contrary to what I originally claimed in #820,
unmockkAllperforms much faster (more operations per second) in the benchmarks in this PR, except when there's nothing to unmock (thenclearAllMocksalso is fast). This means that it's probably a good idea to unmock all after each unit test. This is whatMockKRuledoes if applied as a JUnit 4@Rule. However, before this PR the JUnit 5MockKExtensiondoesn't callunmockkAllafter each test. This PR fixes that.In this way, #820 can now also be closed (after accepting these changes), since it is outdated / incorrect.