@@ -183,8 +183,6 @@ private abstract class StaticMocker(
183
183
184
184
private class MockitoMocker (context : CgContext ) : ObjectMocker(context) {
185
185
186
- private val alreadyMockedModels: MutableSet <UtCompositeModel > = Collections .newSetFromMap(IdentityHashMap ())
187
-
188
186
override fun createMock (model : UtCompositeModel , baseName : String ): CgVariable {
189
187
val modelClass = getClassOf(model.classId)
190
188
val mockObject = newVar(model.classId, baseName = baseName, isMock = true ) { mock(modelClass) }
@@ -195,10 +193,6 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) {
195
193
}
196
194
197
195
fun mockForVariable (model : UtCompositeModel , mockObject : CgVariable ) {
198
- if (! alreadyMockedModels.add(model)) {
199
- return
200
- }
201
-
202
196
for ((executable, values) in model.mocks) {
203
197
val matchers = mockitoArgumentMatchersFor(executable)
204
198
@@ -225,7 +219,12 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) {
225
219
error(" Cannot mock method $executable as it is not accessible from package $testClassPackageName " )
226
220
}
227
221
228
- val results = values.map { variableConstructor.getOrCreateVariable(it) }.toTypedArray()
222
+ val results = values
223
+ .map { value ->
224
+ // Sometimes we need mocks returning itself, e.g. for StringBuilder.append method
225
+ if (value != model) variableConstructor.getOrCreateVariable(value) else mockObject
226
+ }
227
+ .toTypedArray()
229
228
`when `(mockObject[executable](* matchers)).thenReturn(executable.returnType, * results)
230
229
}
231
230
else -> error(" Only MethodId was expected to appear in simple mocker but got $executable " )
0 commit comments