You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When activating CheckUnnecessaryStub and using partial mocking feature, an unnecessary stubb is wrongly detected when a mock which is a property of the object partially mocked. Below is a minimal example to reproduce the issue.
Non working example (multiplier as a property):
importio.mockk.everyimportio.mockk.junit5.MockKExtensionimportio.mockk.mockkimportorg.junit.jupiter.api.extension.ExtendWithimportkotlin.test.Testimportkotlin.test.assertEqualsdata classMultiplier(
valvalue:Int,
)
// Class to be testedclassCalculator(
valmultiplier:Multiplier,
) {
// Call originalfunmultiply(a:Int): Int= a * multiplier.value
}
@ExtendWith(MockKExtension::class)
@MockKExtension.CheckUnnecessaryStubclassCalculatorTest {
@Test
fun`test partial mocking and unnecessary stubbing`() {
val calculator = mockk<Calculator>()
// Partial mock
every { calculator.multiply(2) } answers { callOriginal() }
val multiplier = mockk<Multiplier>()
every { calculator.multiplier } returns multiplier
every { multiplier.value } returns 10
assertEquals(20, calculator.multiply(2))
}
}
Code above raises the following error:
Unnecessary stubbings detected.
Following stubbings are not used, either because there are unnecessary or because tested code doesn't call them :
Calculator(#1).getMultiplier())
java.lang.AssertionError: Unnecessary stubbings detected.
Following stubbings are not used, either because there are unnecessary or because tested code doesn't call them :
Calculator(#1).getMultiplier())
at io.mockk.impl.recording.CommonVerificationAcknowledger.checkUnnecessaryStubHelper(CommonVerificationAcknowledger.kt:79)
at io.mockk.impl.recording.CommonVerificationAcknowledger.checkUnnecessaryStub(CommonVerificationAcknowledger.kt:30)
at io.mockk.MockKDsl.internalCheckUnnecessaryStub(API.kt:287)
at io.mockk.MockKKt.checkUnnecessaryStub(MockK.kt:400)
at io.mockk.junit5.MockKExtension.afterAll(MockKExtension.kt:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1597)
Working Example (multiplier as function)
importio.mockk.everyimportio.mockk.junit5.MockKExtensionimportio.mockk.mockkimportorg.junit.jupiter.api.extension.ExtendWithimportkotlin.test.Testimportkotlin.test.assertEqualsdata classMultiplier(
valvalue:Int,
)
// Class to be testedclassCalculator {
funmultiplier() =Multiplier(5)
// Call originalfunmultiply(a:Int): Int= a * multiplier().value
}
@ExtendWith(MockKExtension::class)
@MockKExtension.CheckUnnecessaryStubclassCalculatorTest {
@Test
fun`test partial mocking and unnecessary stubbing`() {
val calculator = mockk<Calculator>()
// Partial mock
every { calculator.multiply(2) } answers { callOriginal() }
val multiplier = mockk<Multiplier>()
every { calculator.multiplier() } returns multiplier
every { multiplier.value } returns 10
assertEquals(20, calculator.multiply(2))
}
}
The text was updated successfully, but these errors were encountered:
When activating
CheckUnnecessaryStub
and using partial mocking feature, an unnecessary stubb is wrongly detected when a mock which is a property of the object partially mocked. Below is a minimal example to reproduce the issue.Non working example (multiplier as a property):
Code above raises the following error:
Working Example (multiplier as function)
The text was updated successfully, but these errors were encountered: