@@ -7,6 +7,7 @@ import org.utbot.framework.codegen.ForceStaticMocking
7
7
import org.utbot.framework.codegen.JUNIT5_PARAMETERIZED_PACKAGE
8
8
import org.utbot.framework.codegen.Junit4
9
9
import org.utbot.framework.codegen.Junit5
10
+ import org.utbot.framework.codegen.ParametrizedTestSource
10
11
import org.utbot.framework.codegen.RuntimeExceptionTestsBehaviour.PASS
11
12
import org.utbot.framework.codegen.TestNg
12
13
import org.utbot.framework.codegen.model.constructor.builtin.closeMethodIdOrNull
@@ -996,7 +997,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
996
997
" but `${resultModel::class } ` found"
997
998
}
998
999
999
- generateDeepEqualsAssertion (expected, actual)
1000
+ generateDeepEqualsOrNullAssertion (expected, actual)
1000
1001
}
1001
1002
}
1002
1003
}
@@ -1028,16 +1029,41 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1028
1029
return
1029
1030
}
1030
1031
1031
- generateDeepEqualsAssertion (expected, actual)
1032
+ generateDeepEqualsOrNullAssertion (expected, actual)
1032
1033
}
1033
1034
}
1034
1035
}
1035
1036
}
1036
1037
1037
- private fun generateDeepEqualsAssertion (
1038
+ /* *
1039
+ * We can't use standard deepEquals method in parametrized tests
1040
+ * because nullable objects require different asserts.
1041
+ * See https://github.com/UnitTestBot/UTBotJava/issues/252 for more details.
1042
+ */
1043
+ private fun generateDeepEqualsOrNullAssertion (
1038
1044
expected : CgValue ,
1039
- actual : CgVariable
1045
+ actual : CgVariable ,
1040
1046
) {
1047
+ when (parameterizedTestSource) {
1048
+ ParametrizedTestSource .DO_NOT_PARAMETRIZE ->
1049
+ currentBlock = currentBlock.addAll(generateDeepEqualsAssertion(expected, actual))
1050
+ ParametrizedTestSource .PARAMETRIZE -> {
1051
+ val assertNullStmt = listOf (testFrameworkManager.assertions[testFramework.assertNull](actual).toStatement())
1052
+ currentBlock = currentBlock.add(
1053
+ CgIfStatement (
1054
+ CgEqualTo (expected, nullLiteral()),
1055
+ assertNullStmt,
1056
+ generateDeepEqualsAssertion(expected, actual)
1057
+ )
1058
+ )
1059
+ }
1060
+ }
1061
+ }
1062
+
1063
+ private fun generateDeepEqualsAssertion (
1064
+ expected : CgValue ,
1065
+ actual : CgVariable ,
1066
+ ): List <CgStatement > {
1041
1067
require(expected is CgVariable ) {
1042
1068
" Expected value have to be Literal or Variable but `${expected::class } ` found"
1043
1069
}
@@ -1051,7 +1077,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1051
1077
depth = 0 ,
1052
1078
visitedModels = hashSetOf()
1053
1079
)
1054
- currentBlock = currentBlock.addAll(statements.dropLastWhile { it is CgEmptyLine })
1080
+
1081
+ return statements.dropLastWhile { it is CgEmptyLine }
1055
1082
}
1056
1083
1057
1084
private fun recordActualResult () {
@@ -1166,7 +1193,9 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1166
1193
}
1167
1194
1168
1195
// TODO: orientation on arbitrary execution may be misleading, but what is the alternative?
1169
- val arbitraryExecution = utTestCase.executions.firstOrNull { it.result is UtExecutionSuccess }
1196
+ // may be a heuristic to select a model with minimal number of internal nulls should be used
1197
+ val arbitraryExecution = utTestCase.executions
1198
+ .firstOrNull { it.result is UtExecutionSuccess && (it.result as UtExecutionSuccess ).model !is UtNullModel }
1170
1199
? : utTestCase.executions.first()
1171
1200
1172
1201
return withTestMethodScope(arbitraryExecution) {
@@ -1217,7 +1246,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1217
1246
// record result and generate result assertions
1218
1247
recordActualResult()
1219
1248
generateAssertionsForParameterizedTest()
1220
-
1221
1249
}
1222
1250
1223
1251
methodType = PARAMETRIZED
0 commit comments