Skip to content

Commit 9b95f55

Browse files
authored
Enable Java assertions in framework integration tests (#539)
Added "-ea" command line argument to the java invocation used to run integration tests in `utbot-framework`.
1 parent d5d7ea5 commit 9b95f55

File tree

5 files changed

+72
-12
lines changed

5 files changed

+72
-12
lines changed

utbot-framework/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ test {
6565
jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009'
6666
}
6767
}
68+
69+
compileKotlin {
70+
kotlinOptions {
71+
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
72+
}
73+
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/Domain.kt

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ sealed class TestFramework(
223223
executionInvoke: String,
224224
classPath: String,
225225
classesNames: List<String>,
226-
buildDirectory: String
226+
buildDirectory: String,
227+
additionalArguments: List<String>
227228
): List<String>
228229

229230
override fun toString() = displayName
@@ -298,16 +299,23 @@ object TestNg : TestFramework(displayName = "TestNG") {
298299
simpleName = "DataProvider"
299300
)
300301

302+
@OptIn(ExperimentalStdlibApi::class)
301303
override fun getRunTestsCommand(
302304
executionInvoke: String,
303305
classPath: String,
304306
classesNames: List<String>,
305-
buildDirectory: String
307+
buildDirectory: String,
308+
additionalArguments: List<String>
306309
): List<String> {
307310
// TestNg requires a specific xml to run with
308311
writeXmlFileForTestSuite(buildDirectory, classesNames)
309312

310-
return listOf(executionInvoke, "$mainPackage.TestNG", "$buildDirectory${File.separator}$testXmlName")
313+
return buildList {
314+
add(executionInvoke)
315+
addAll(additionalArguments)
316+
add("$mainPackage.TestNG")
317+
add("$buildDirectory${File.separator}$testXmlName")
318+
}
311319
}
312320

313321
private fun writeXmlFileForTestSuite(buildDirectory: String, testsNames: List<String>) {
@@ -375,12 +383,19 @@ object Junit4 : TestFramework("JUnit4") {
375383
)
376384
}
377385

386+
@OptIn(ExperimentalStdlibApi::class)
378387
override fun getRunTestsCommand(
379388
executionInvoke: String,
380389
classPath: String,
381390
classesNames: List<String>,
382-
buildDirectory: String
383-
): List<String> = listOf(executionInvoke, "$mainPackage.runner.JUnitCore") + classesNames
391+
buildDirectory: String,
392+
additionalArguments: List<String>
393+
): List<String> = buildList {
394+
add(executionInvoke)
395+
addAll(additionalArguments)
396+
add("$mainPackage.runner.JUnitCore")
397+
addAll(classesNames)
398+
}
384399
}
385400

386401
object Junit5 : TestFramework("JUnit5") {
@@ -464,16 +479,20 @@ object Junit5 : TestFramework("JUnit5") {
464479
private const val junitVersion = "1.7.1" // TODO read it from gradle.properties
465480
private const val platformJarName: String = "junit-platform-console-standalone-$junitVersion.jar"
466481

482+
@OptIn(ExperimentalStdlibApi::class)
467483
override fun getRunTestsCommand(
468484
executionInvoke: String,
469485
classPath: String,
470486
classesNames: List<String>,
471-
buildDirectory: String
472-
): List<String> =
473-
listOf(
474-
executionInvoke,
475-
"-jar", classPath.split(File.pathSeparator).single { platformJarName in it },
476-
) + isolateCommandLineArgumentsToArgumentFile(listOf("-cp", classPath).plus(classesNames.map { "-c=$it" }))
487+
buildDirectory: String,
488+
additionalArguments: List<String>
489+
): List<String> = buildList {
490+
add(executionInvoke)
491+
addAll(additionalArguments)
492+
add("-jar")
493+
add(classPath.split(File.pathSeparator).single { platformJarName in it })
494+
add(isolateCommandLineArgumentsToArgumentFile(listOf("-cp", classPath).plus(classesNames.map { "-c=$it" })))
495+
}
477496
}
478497

479498
enum class RuntimeExceptionTestsBehaviour(
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.utbot.examples.codegen
2+
3+
import org.junit.jupiter.api.Test
4+
import org.utbot.examples.UtValueTestCaseChecker
5+
import org.utbot.examples.eq
6+
import org.utbot.examples.isException
7+
8+
class JavaAssertTest : UtValueTestCaseChecker(testClass = JavaAssert::class){
9+
@Test
10+
fun testAssertPositive() {
11+
checkWithException(
12+
JavaAssert::assertPositive,
13+
eq(2),
14+
{ value, result -> value > 0 && result.isSuccess && result.getOrNull() == value },
15+
{ value, result -> value <= 0 && result.isException<java.lang.AssertionError>() }
16+
)
17+
}
18+
}

utbot-framework/src/test/kotlin/org/utbot/framework/codegen/CompilationAndRunUtils.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,17 @@ fun runTests(
5656
) {
5757
val classpath = System.getProperty("java.class.path") + File.pathSeparator + buildDirectory
5858
val executionInvoke = generatedLanguage.executorInvokeCommand
59+
val additionalArguments = listOf(
60+
"-ea", // Enable assertions
61+
)
5962

60-
val command = testFramework.getRunTestsCommand(executionInvoke, classpath, testsNames, buildDirectory)
63+
val command = testFramework.getRunTestsCommand(
64+
executionInvoke,
65+
classpath,
66+
testsNames,
67+
buildDirectory,
68+
additionalArguments
69+
)
6170

6271
logger.trace { "Command to run test: [${command.joinToString(" ")}]" }
6372

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.utbot.examples.codegen;
2+
3+
public class JavaAssert {
4+
public int assertPositive(int value) {
5+
assert value > 0;
6+
return value;
7+
}
8+
}

0 commit comments

Comments
 (0)