Skip to content

Commit d000195

Browse files
Correct two regressions in Spring codegen (#2247)
1 parent ecd9a4c commit d000195

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/MockFrameworkManager.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ private abstract class StaticMocker(
183183

184184
private class MockitoMocker(context: CgContext) : ObjectMocker(context) {
185185

186-
private val alreadyMockedModels: MutableSet<UtCompositeModel> = Collections.newSetFromMap(IdentityHashMap())
187-
188186
override fun createMock(model: UtCompositeModel, baseName: String): CgVariable {
189187
val modelClass = getClassOf(model.classId)
190188
val mockObject = newVar(model.classId, baseName = baseName, isMock = true) { mock(modelClass) }
@@ -195,10 +193,6 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) {
195193
}
196194

197195
fun mockForVariable(model: UtCompositeModel, mockObject: CgVariable) {
198-
if (!alreadyMockedModels.add(model)) {
199-
return
200-
}
201-
202196
for ((executable, values) in model.mocks) {
203197
val matchers = mockitoArgumentMatchersFor(executable)
204198

@@ -225,7 +219,12 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) {
225219
error("Cannot mock method $executable as it is not accessible from package $testClassPackageName")
226220
}
227221

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()
229228
`when`(mockObject[executable](*matchers)).thenReturn(executable.returnType, *results)
230229
}
231230
else -> error("Only MethodId was expected to appear in simple mocker but got $executable")

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringUnitTestClassConstructor.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,32 @@ import org.utbot.framework.plugin.api.util.objectClassId
2121

2222
class CgSpringUnitTestClassConstructor(context: CgContext) : CgAbstractSpringTestClassConstructor(context) {
2323

24+
private var additionalMethodsRequired: Boolean = false
25+
2426
private lateinit var mockitoCloseableVariable: CgValue
2527

2628
override fun constructClassFields(testClassModel: SpringTestClassModel): List<CgFieldDeclaration> {
2729
val fields = mutableListOf<CgFieldDeclaration>()
28-
val mockedFields = constructFieldsWithAnnotation(testClassModel.thisInstanceDependentMocks, mockClassId)
2930

30-
if (mockedFields.isNotEmpty()) {
31-
fields += constructFieldsWithAnnotation(testClassModel.thisInstanceModels, injectMocksClassId)
32-
fields += mockedFields
31+
if (testClassModel.thisInstanceDependentMocks.isNotEmpty()) {
32+
val mockedFields = constructFieldsWithAnnotation(testClassModel.thisInstanceDependentMocks, mockClassId)
33+
val injectingMocksFields = constructFieldsWithAnnotation(testClassModel.thisInstanceModels, injectMocksClassId)
3334

35+
fields += injectingMocksFields
36+
fields += mockedFields
3437
fields += constructMockitoCloseables()
38+
39+
additionalMethodsRequired = true
3540
}
3641

3742
return fields
3843
}
3944

4045
override fun constructAdditionalMethods(): CgMethodsCluster {
46+
if (!additionalMethodsRequired) {
47+
return CgMethodsCluster(header = null, content = emptyList(),)
48+
}
49+
4150
importIfNeeded(openMocksMethodId)
4251

4352
val openMocksCall = CgMethodCall(

0 commit comments

Comments
 (0)