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
interfaceI {
funf(): Int
}
classITest {
@Mock val mockI:I= mock(classOf<I>())
@Test funf() {
every { mockI.f() }.returnsMany(1, 2)
every { mockI.f() }.returnsMany(3, 4)
val expected =listOf(1, 2, 3, 4)
val actual =listOf(mockI.f(), mockI.f(), mockI.f(), mockI.f())
assertEquals(expected, actual)
verify { mockI.f() }.wasInvoked(exactly =4.times)
verifyNoUnverifiedExpectations(mockI)
verifyNoUnmetExpectations(mockI)
}
}
Background context
I originally wanted to test what happens if one of the mapping inside a loop was failing. I picked the one to fail to be the first one, and continued to set up the mocks like this:
every { mapper.map(any()) }.throws(ex)
every { mapper.map(any()) }.returnsMany(obj1, obj2)
this didn't work, because throws will just throw every time. Or at least that's what I thought (the error was too long so I didn't read it :).
So I went on to check the code and based on ClosedBlockingStub's behavior, I set it up like this:
every { mapper.map(any()) }.throwsMany(ex)
every { mapper.map(any()) }.returnsMany(obj1, obj2)
It still didn't work, so I read the error message, and to all my confusion the solution was to swap them:
every { mapper.map(any()) }.returnsMany(obj1, obj2)
every { mapper.map(any()) }.throwsMany(ex)
Made the first iteration of the mapping loop throw, and 2nd and 3rd return the objects respectively. To simplify the repro I used returnsMany twice. Behavior is the same.
The text was updated successfully, but these errors were encountered:
Background context
I originally wanted to test what happens if one of the mapping inside a loop was failing. I picked the one to fail to be the first one, and continued to set up the mocks like this:
this didn't work, because
throws
will just throw every time. Or at least that's what I thought (the error was too long so I didn't read it :).So I went on to check the code and based on
ClosedBlockingStub
's behavior, I set it up like this:It still didn't work, so I read the error message, and to all my confusion the solution was to swap them:
Made the first iteration of the mapping loop throw, and 2nd and 3rd return the objects respectively. To simplify the repro I used
returnsMany
twice. Behavior is the same.The text was updated successfully, but these errors were encountered: