Skip to content

Commit 6b40a44

Browse files
Improve autowired models naming (#2313)
1 parent 53bbc46 commit 6b40a44

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,13 @@ object SpringModelUtils {
6161

6262
fun UtModel.isAutowiredFromContext(): Boolean =
6363
this is UtAssembleModel && this.instantiationCall.instance is UtSpringContextModel
64+
65+
fun UtModel.getBeanNameOrNull(): String? = if (isAutowiredFromContext()) {
66+
this as UtAssembleModel
67+
val beanNameParam = this.instantiationCall.params.single()
68+
val paramValue = (beanNameParam as? UtPrimitiveModel)?.value
69+
paramValue.toString()
70+
} else {
71+
null
72+
}
6473
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.utbot.framework.codegen.domain.models.SpringTestClassModel
1717
import org.utbot.framework.codegen.domain.models.builders.TypedModelWrappers
1818
import org.utbot.framework.plugin.api.ClassId
1919
import org.utbot.framework.plugin.api.UtSpringContextModel
20+
import org.utbot.framework.plugin.api.util.SpringModelUtils.getBeanNameOrNull
2021
import org.utbot.framework.plugin.api.util.id
2122
import java.lang.Exception
2223

@@ -90,8 +91,11 @@ abstract class CgAbstractSpringTestClassConstructor(context: CgContext):
9091

9192
val constructedDeclarations = mutableListOf<CgFieldDeclaration>()
9293
for ((classId, listOfUtModels) in groupedModelsByClassId) {
93-
val model = listOfUtModels.firstOrNull() ?: continue
94-
val createdVariable = variableConstructor.getOrCreateVariable(model.model) as? CgVariable
94+
val modelWrapper = listOfUtModels.firstOrNull() ?: continue
95+
val model = modelWrapper.model
96+
val baseVarName = model.getBeanNameOrNull()
97+
98+
val createdVariable = variableConstructor.getOrCreateVariable(model, baseVarName) as? CgVariable
9599
?: error("`UtCompositeModel` model was expected, but $model was found")
96100

97101
val declaration = CgDeclaration(classId, variableName = createdVariable.name, initializer = null)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import org.utbot.framework.plugin.api.UtCompositeModel
1414
import org.utbot.framework.plugin.api.UtModel
1515
import org.utbot.framework.plugin.api.UtSpringContextModel
1616
import org.utbot.framework.plugin.api.isMockModel
17-
import org.utbot.framework.plugin.api.util.SpringModelUtils.applicationContextClassId
1817
import org.utbot.framework.plugin.api.util.SpringModelUtils.isAutowiredFromContext
1918
import org.utbot.framework.plugin.api.util.stringClassId
2019

0 commit comments

Comments
 (0)