Skip to content

Commit 01c55b9

Browse files
authored
Emit UtError when there are no beans for this instance during integration test generation (#2399)
Emit UtError when there are no beans for this instance during integration test generation
1 parent 2b66d12 commit 01c55b9

File tree

2 files changed

+25
-4
lines changed
  • utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api
  • utbot-framework/src/main/kotlin/org/utbot/engine

2 files changed

+25
-4
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,14 @@ class SpringApplicationContext(
15371537
exception
15381538
)
15391539
}.orEmpty() + super.getErrors()
1540+
1541+
fun getBeansAssignableTo(classId: ClassId): List<BeanDefinitionData> = beanDefinitions.filter { beanDef ->
1542+
// some bean classes may fail to load
1543+
runCatching {
1544+
val beanClass = ClassId(beanDef.beanTypeName).jClass
1545+
classId.jClass.isAssignableFrom(beanClass)
1546+
}.getOrElse { false }
1547+
}
15401548
}
15411549

15421550
enum class SpringTestType(

utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import org.utbot.framework.util.calculateSize
4040
import org.utbot.framework.util.convertToAssemble
4141
import org.utbot.framework.util.graph
4242
import org.utbot.framework.util.sootMethod
43-
import org.utbot.framework.plugin.api.SpringSettings.*
4443
import org.utbot.fuzzer.*
4544
import org.utbot.fuzzing.*
4645
import org.utbot.fuzzing.providers.FieldValueProvider
@@ -388,9 +387,24 @@ class UtBotSymbolicEngine(
388387
val attemptsLimit = UtSettings.fuzzingMaxAttempts
389388
val names = graph.body.method.tags.filterIsInstance<ParamNamesTag>().firstOrNull()?.names ?: emptyList()
390389
var testEmittedByFuzzer = 0
390+
391+
if (applicationContext is SpringApplicationContext &&
392+
applicationContext.springTestType == SpringTestType.INTEGRATION_TEST &&
393+
applicationContext.getBeansAssignableTo(methodUnderTest.classId).isEmpty()) {
394+
val fullConfigDisplayName = (applicationContext.springSettings as? SpringSettings.PresentSpringSettings)
395+
?.configuration?.fullDisplayName
396+
val errorDescription = "No beans of type ${methodUnderTest.classId.name} are found. " +
397+
"Try choosing different Spring configuration or adding beans to $fullConfigDisplayName"
398+
emit(UtError(
399+
errorDescription,
400+
IllegalStateException(errorDescription)
401+
))
402+
return@flow
403+
}
404+
391405
val valueProviders = ValueProvider.of(defaultValueProviders(defaultIdGenerator))
392406
.letIf(applicationContext is SpringApplicationContext
393-
&& applicationContext.springSettings is PresentSpringSettings
407+
&& applicationContext.springTestType == SpringTestType.INTEGRATION_TEST
394408
) { provider ->
395409
val relevantRepositories = concreteExecutor.getRelevantSpringRepositories(methodUnderTest.classId)
396410
logger.info { "Detected relevant repositories for class ${methodUnderTest.classId}: $relevantRepositories" }
@@ -413,8 +427,7 @@ class UtBotSymbolicEngine(
413427
val springBeanValueProvider = SpringBeanValueProvider(
414428
defaultIdGenerator,
415429
beanNameProvider = { classId ->
416-
(applicationContext as SpringApplicationContext).beanDefinitions
417-
.filter { it.beanTypeName == classId.name }
430+
(applicationContext as SpringApplicationContext).getBeansAssignableTo(classId)
418431
.map { it.beanName }
419432
},
420433
relevantRepositories = relevantRepositories

0 commit comments

Comments
 (0)