@@ -3,12 +3,8 @@ package org.utbot.framework.codegen.model.constructor.tree
3
3
import org.utbot.common.PathUtil
4
4
import org.utbot.common.packageName
5
5
import org.utbot.engine.isStatic
6
- import org.utbot.framework.codegen.ForceStaticMocking
7
- import org.utbot.framework.codegen.JUNIT5_PARAMETERIZED_PACKAGE
8
- import org.utbot.framework.codegen.Junit4
9
- import org.utbot.framework.codegen.Junit5
6
+ import org.utbot.framework.codegen.*
10
7
import org.utbot.framework.codegen.RuntimeExceptionTestsBehaviour.PASS
11
- import org.utbot.framework.codegen.TestNg
12
8
import org.utbot.framework.codegen.model.constructor.builtin.closeMethodIdOrNull
13
9
import org.utbot.framework.codegen.model.constructor.builtin.forName
14
10
import org.utbot.framework.codegen.model.constructor.builtin.getClass
@@ -996,7 +992,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
996
992
" but `${resultModel::class } ` found"
997
993
}
998
994
999
- generateDeepEqualsAssertion (expected, actual)
995
+ generateDeepEqualsOrNullAssertion (expected, actual)
1000
996
}
1001
997
}
1002
998
}
@@ -1028,16 +1024,40 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1028
1024
return
1029
1025
}
1030
1026
1031
- generateDeepEqualsAssertion (expected, actual)
1027
+ generateDeepEqualsOrNullAssertion (expected, actual)
1032
1028
}
1033
1029
}
1034
1030
}
1035
1031
}
1036
1032
1037
- private fun generateDeepEqualsAssertion (
1033
+ /* *
1034
+ * We can't use standard deepEquals method in parametrized tests
1035
+ * because nullable objects require different asserts.
1036
+ * See https://github.com/UnitTestBot/UTBotJava/issues/252 for more details.
1037
+ */
1038
+ private fun generateDeepEqualsOrNullAssertion (
1038
1039
expected : CgValue ,
1039
- actual : CgVariable
1040
+ actual : CgVariable ,
1040
1041
) {
1042
+ when (parameterizedTestSource) {
1043
+ ParametrizedTestSource .DO_NOT_PARAMETRIZE ->
1044
+ currentBlock = currentBlock.addAll(generateDeepEqualsAssertion(expected, actual))
1045
+ ParametrizedTestSource .PARAMETRIZE -> {
1046
+ val trueStmts = listOf<CgStatement >(
1047
+ testFrameworkManager.assertions[testFramework.assertNull](actual).toStatement()
1048
+ )
1049
+ val falseStmts = generateDeepEqualsAssertion(expected, actual)
1050
+ currentBlock = currentBlock.add(
1051
+ CgIfStatement (CgEqualTo (expected, nullLiteral()), trueStmts, falseStmts)
1052
+ )
1053
+ }
1054
+ }
1055
+ }
1056
+
1057
+ private fun generateDeepEqualsAssertion (
1058
+ expected : CgValue ,
1059
+ actual : CgVariable ,
1060
+ ): List <CgStatement > {
1041
1061
require(expected is CgVariable ) {
1042
1062
" Expected value have to be Literal or Variable but `${expected::class } ` found"
1043
1063
}
@@ -1051,7 +1071,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1051
1071
depth = 0 ,
1052
1072
visitedModels = hashSetOf()
1053
1073
)
1054
- currentBlock = currentBlock.addAll(statements.dropLastWhile { it is CgEmptyLine })
1074
+
1075
+ return statements.dropLastWhile { it is CgEmptyLine }
1055
1076
}
1056
1077
1057
1078
private fun recordActualResult () {
@@ -1166,7 +1187,9 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1166
1187
}
1167
1188
1168
1189
// TODO: orientation on arbitrary execution may be misleading, but what is the alternative?
1169
- val arbitraryExecution = utTestCase.executions.firstOrNull { it.result is UtExecutionSuccess }
1190
+ // may be a heuristic to select a model with minimal number of internal nulls should be used
1191
+ val arbitraryExecution = utTestCase.executions
1192
+ .firstOrNull { it.result is UtExecutionSuccess && (it.result as UtExecutionSuccess ).model !is UtNullModel }
1170
1193
? : utTestCase.executions.first()
1171
1194
1172
1195
return withTestMethodScope(arbitraryExecution) {
@@ -1217,7 +1240,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1217
1240
// record result and generate result assertions
1218
1241
recordActualResult()
1219
1242
generateAssertionsForParameterizedTest()
1220
-
1221
1243
}
1222
1244
1223
1245
methodType = PARAMETRIZED
0 commit comments