@@ -24,7 +24,6 @@ import org.utbot.framework.codegen.domain.models.ClassModels
2424import org.utbot.framework.codegen.domain.models.SpringTestClassModel
2525import org.utbot.framework.plugin.api.ClassId
2626import org.utbot.framework.plugin.api.UtCompositeModel
27- import org.utbot.framework.plugin.api.UtModel
2827import org.utbot.framework.plugin.api.util.id
2928import org.utbot.framework.plugin.api.util.objectClassId
3029
@@ -45,6 +44,8 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
4544 fields + = constructClassFields(testClassModel.injectedMockModels, injectMocksClassId)
4645 fields + = mockedFields
4746
47+ clearUnwantedVariableModels()
48+
4849 val (closeableField, closeableMethods) = constructMockitoCloseables()
4950 fields + = closeableField
5051 methodRegions + = closeableMethods
@@ -117,6 +118,28 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
117118 return constructedDeclarations
118119 }
119120
121+ /* *
122+ * Clears the results of variable instantiations that occured
123+ * when we create class variables with specific annotations.
124+ * Actually, only mentioned variables should be stored in `valueByModelId`.
125+ *
126+ * This is a kind of HACK.
127+ * It is better to distinguish creating variable by model with all
128+ * related side effects and just creating a variable definition,
129+ * but it will take very long time to do it now.
130+ */
131+ private fun clearUnwantedVariableModels () {
132+ val whiteListOfModels =
133+ listOf (
134+ variableConstructor.mockedModelsVariables,
135+ variableConstructor.injectedMocksModelsVariables
136+ ).flatMap { modelSet -> modelSet.keys.map { models -> context.getIdByModel(models.first()) } }
137+
138+ valueByModelId
139+ .filter { it.key !in whiteListOfModels }
140+ .forEach { valueByModelId.remove(it.key) }
141+ }
142+
120143 private fun constructMockitoCloseables (): Pair <CgFieldDeclaration , CgMethodsCluster > {
121144 val mockitoCloseableVarName = " mockitoCloseable"
122145 val mockitoCloseableVarType = java.lang.AutoCloseable ::class .id
0 commit comments