Skip to content

Commit a6b5ff7

Browse files
authored
Fix not defined variables in test method in Spring project #2107 (#2124)
1 parent d078ba8 commit a6b5ff7

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import org.utbot.framework.codegen.domain.models.ClassModels
2424
import org.utbot.framework.codegen.domain.models.SpringTestClassModel
2525
import org.utbot.framework.plugin.api.ClassId
2626
import org.utbot.framework.plugin.api.UtCompositeModel
27-
import org.utbot.framework.plugin.api.UtModel
2827
import org.utbot.framework.plugin.api.util.id
2928
import 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

Comments
 (0)