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
Lifecycle hooks with mockk do not work properly on object
and java classes with static functions
#3962
Comments
object
and java classes with static functions
FWIW: This seems to be a regression, as with |
this is a well known problem with mocking static functions - it affects the whole JVM, not just one thread. so, whenever tests happen to run in parallel with this mocking and use the function being mocked, they will be broken. IMO this has nothing to do with kotest. I can reproduce the issue with plain Kotlin. Pls correct me if I am wrong. |
Our tests are not configured to run in parallel. Could it be that Kotest runs tests in parallel by default in the new version? |
No, the default is sequential execution: https://kotest.io/docs/framework/project-config.html#parallelism |
we run kotest in gradle builds, and by default gradle runs different specs in parallel. So we can annotate |
Getting rid of the static mocks would be the best solution, as it would also allow to run the spec in parallel.
@AlexCue987 Do you have any source for this, the Kotest documentation (as linked by @OliverO2) specifies that by default it is not the case. Is gradle overriding kotest somehow? |
my understanding was that the following was being set under the hood:
|
Adding the annotation funny enough: for the example I provided, parallelism would fix the issue, as we would run into a race condition (tested with changing the |
Then I was wrong, and this issue is not about parallel execution. Let's investigate more. Maybe:
Has the following been invoked:
|
Thanks for your input, Alex!
I still suspect the problem to be a @TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class MockedTestJunit {
@Test
@Order(1)
fun usingAMockedObject() {
Assertions.assertEquals(4, TestToMock.intSum(3, 1))
}
@Test
@Order(2)
fun whenAnswerProvidedObject() {
every { TestToMock.intSum(any(), any()) } returns 100
Assertions.assertEquals(100, TestToMock.intSum(3, 1))
}
@Test
@Order(3)
fun usingAMockedStaticFun() {
Assertions.assertEquals(4, TestStaticOnly.intSum(3, 1))
}
@Test
@Order(4)
fun whenAnswerProvidedStaticFun() {
every { TestStaticOnly.intSum(any(), any()) } returns 100
Assertions.assertEquals(100, TestStaticOnly.intSum(3, 1))
}
companion object {
@JvmStatic
@BeforeAll
fun doMock() {
mockkObject(TestToMock)
mockkStatic(TestStaticOnly::class)
println("Mocked")
}
@JvmStatic
@AfterAll
fun unmock() {
unmockkObject(TestToMock)
unmockkStatic(TestStaticOnly::class)
println("Unmocked")
}
}
} and @TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class RealTestJunit {
@Test
@Order(10)
fun usingAnAlreadyUnmockedObject() {
Assertions.assertEquals(4, TestToMock.intSum(3, 1))
}
@Test
@Order(11)
fun usingAnAlreadyUnmockedStaticFun() {
Assertions.assertEquals(4, TestStaticOnly.intSum(3, 1))
}
} |
returning to "Adding the annotation DoNotParallelize does not change anything" - I would expect that to fix the issue. This is where we should investigate further. |
The default behavior is sequencial, as Oliver mentioned here: #3962 (comment) |
I'm having pretty much the same issue. I also don't run tests in parallel (I did not set the I'm definitly intereseted in the solution for this problem. |
Which version of Kotest are you using
5.8.1
Which version of mockk are you using
1.13.10
Steps to reproduce:
Creating an object which will be mocked for some tests:
or a java file with a static function:
Having 2 specs which use this object or java static function:
beforeSpec
and unmocked viaafterSpec
Executing each spec on its own does work as expected.
Having both in the same package and executing the package (or whole test suite) will crash the second test (
RealTest
) which should use an unmocked state with this assertion:java.lang.AssertionError: expected:<4> but was:<100>
.Note: It is important, that the
RealTest
is executed after the mocked + unmocked test for this crash to happen!It seems there is a problem with mocking/unmocking objects and static functions in combination with lifecycle callbacks.
The text was updated successfully, but these errors were encountered: