Skip to content

Commit 9c7a052

Browse files
committed
Assert explicitly thrown exceptions in generated tests
1 parent a21a686 commit 9c7a052

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import org.utbot.framework.plugin.api.UtEnumConstantModel
107107
import org.utbot.framework.plugin.api.UtExecution
108108
import org.utbot.framework.plugin.api.UtExecutionFailure
109109
import org.utbot.framework.plugin.api.UtExecutionSuccess
110+
import org.utbot.framework.plugin.api.UtExplicitlyThrownException
110111
import org.utbot.framework.plugin.api.UtMethod
111112
import org.utbot.framework.plugin.api.UtModel
112113
import org.utbot.framework.plugin.api.UtNewInstanceInstrumentation
@@ -292,7 +293,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
292293
// we cannot generate any assertions for constructor testing
293294
// but we need to generate a constructor call
294295
val constructorCall = currentExecutable as ConstructorId
295-
currentExecution!!.result
296+
val currentExecution = currentExecution!!
297+
currentExecution.result
296298
.onSuccess {
297299
methodType = SUCCESSFUL
298300

@@ -308,15 +310,16 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
308310
}
309311
}
310312
.onFailure { exception ->
311-
processExecutionFailure(exception)
313+
processExecutionFailure(currentExecution, exception)
312314
}
313315
}
314316
is BuiltinMethodId -> error("Unexpected BuiltinMethodId $currentExecutable while generating result assertions")
315317
is MethodId -> {
316318
emptyLineIfNeeded()
317319
val method = currentExecutable as MethodId
320+
val currentExecution = currentExecution!!
318321
// build assertions
319-
currentExecution!!.result
322+
currentExecution.result
320323
.onSuccess { result ->
321324
methodType = SUCCESSFUL
322325

@@ -330,13 +333,13 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
330333
}
331334
}
332335
.onFailure { exception ->
333-
processExecutionFailure(exception)
336+
processExecutionFailure(currentExecution, exception)
334337
}
335338
}
336339
}
337340
}
338341

339-
private fun processExecutionFailure(exception: Throwable) {
342+
private fun processExecutionFailure(execution: UtExecution, exception: Throwable) {
340343
val methodInvocationBlock = {
341344
with(currentExecutable) {
342345
when (this) {
@@ -346,7 +349,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
346349
}
347350
}
348351

349-
if (shouldTestPassWithExpectedException(exception)) {
352+
if (shouldTestPassWithException(execution, exception)) {
350353
testFrameworkManager.expectException(exception::class.id) {
351354
methodInvocationBlock()
352355
}
@@ -373,11 +376,13 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
373376
methodInvocationBlock()
374377
}
375378

376-
private fun shouldTestPassWithExpectedException(exception: Throwable): Boolean {
379+
private fun shouldTestPassWithException(execution: UtExecution, exception: Throwable): Boolean {
377380
// tests with timeout or crash should be processed differently
378381
if (exception is TimeoutException || exception is ConcreteExecutionFailureException) return false
379382

380-
return runtimeExceptionTestsBehaviour == PASS || exception !is RuntimeException
383+
val exceptionRequiresAssert = exception !is RuntimeException || runtimeExceptionTestsBehaviour == PASS
384+
val exceptionIsExplicit = execution.result is UtExplicitlyThrownException
385+
return exceptionRequiresAssert || exceptionIsExplicit
381386
}
382387

383388
private fun writeWarningAboutTimeoutExceeding() {

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/TestFrameworkManager.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ internal abstract class TestFrameworkManager(val context: CgContext)
147147

148148
fun assertBoolean(actual: CgExpression) = assertBoolean(expected = true, actual)
149149

150-
// Exception expectation is very different in JUnit4 and JUnit5
150+
// Exception expectation differs between test frameworks
151151
// JUnit4 requires to add a specific argument to the test method annotation
152-
// JUnit5 requires to use method assertThrows()
152+
// JUnit5 requires using method assertThrows()
153+
// TestNg allows both approaches, we use similar to JUnit5
153154
abstract fun expectException(exception: ClassId, block: () -> Unit)
154155

155156
open fun setTestExecutionTimeout(timeoutMs: Long) {

0 commit comments

Comments
 (0)