Skip to content

Commit ddff0b0

Browse files
committed
Be able to invoke all public methods in integration tests
1 parent dd54e53 commit ddff0b0

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringIntegrationTestConcreteExecutionContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class SpringIntegrationTestConcreteExecutionContext(
8787
springApplicationContext.getBeansAssignableTo(classId).map { it.beanName }
8888
},
8989
relevantRepositories = relevantRepositories
90-
).withFallback(anyObjectValueProvider(idGenerator))
90+
).withFallback(anyObjectValueProvider(idGenerator, isSpringProject = true))
9191

9292
return delegateContext.tryCreateValueProvider(concreteExecutor, classUnderTest, idGenerator)
9393
.except { p -> p is ObjectValueProvider }

utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/Objects.kt

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ private fun isIgnored(type: ClassId): Boolean {
3131
|| (type.isInner && !type.isStatic)
3232
}
3333

34-
fun anyObjectValueProvider(idGenerator: IdentityPreservingIdGenerator<Int>) =
35-
ObjectValueProvider(idGenerator).letIf(UtSettings.fuzzingImplementationOfAbstractClasses) { ovp ->
34+
fun anyObjectValueProvider(idGenerator: IdentityPreservingIdGenerator<Int>, isSpringProject: Boolean = false) =
35+
ObjectValueProvider(idGenerator, isSpringProject).letIf(UtSettings.fuzzingImplementationOfAbstractClasses) { ovp ->
3636
ovp.withFallback(AbstractsObjectValueProvider(idGenerator))
3737
}
3838

3939
class ObjectValueProvider(
4040
val idGenerator: IdGenerator<Int>,
41+
private val isSpringProject: Boolean,
4142
) : ValueProvider<FuzzedType, FuzzedValue, FuzzedDescription> {
4243

4344
override fun accept(type: FuzzedType) = !isIgnored(type.classId)
@@ -100,16 +101,18 @@ class ObjectValueProvider(
100101
}
101102
}
102103
}
103-
findAllPublicMethods(description, classId, description.description.packageName).forEach { md ->
104-
yield(Routine.Call(md.parameterTypes) { self, values ->
105-
val model = self.model as UtAssembleModel
106-
model.modificationsChain as MutableList +=
107-
UtExecutableCallModel(
108-
model,
109-
md.executable,
110-
values.map { it.model }
111-
)
112-
})
104+
if (isSpringProject) {
105+
findAllPublicMethods(description, classId, description.description.packageName).forEach { md ->
106+
yield(Routine.Call(md.parameterTypes) { self, values ->
107+
val model = self.model as UtAssembleModel
108+
model.modificationsChain as MutableList +=
109+
UtExecutableCallModel(
110+
model,
111+
md.method.executableId,
112+
values.map { it.model }
113+
)
114+
})
115+
}
113116
}
114117
},
115118
empty = nullRoutine(classId)
@@ -233,7 +236,7 @@ internal class FieldDescription(
233236
internal class MethodDescription(
234237
val name: String,
235238
val parameterTypes: List<FuzzedType>,
236-
val executable: ExecutableId
239+
val method: Method
237240
)
238241

239242
internal fun findAccessibleModifiableFields(description: FuzzedDescription?, classId: ClassId, packageName: String?): List<FieldDescription> {
@@ -264,6 +267,7 @@ internal fun findAllPublicMethods(
264267
): List<MethodDescription> =
265268
classId.jClass.declaredMethods.mapNotNull { method ->
266269
if (isAccessible(method, packageName)) {
270+
if (method.genericParameterTypes.any { it !is Class<*> }) return@mapNotNull null
267271
val parameterTypes =
268272
method
269273
.parameterTypes
@@ -279,11 +283,16 @@ internal fun findAllPublicMethods(
279283
MethodDescription(
280284
name = method.name,
281285
parameterTypes = parameterTypes,
282-
executable = method.executableId
286+
method = method
283287
)
284288
} else null
285289
}
286290

291+
internal fun List<MethodDescription>.removeSetters(): List<MethodDescription> =
292+
filterNot { md ->
293+
md.method.name.startsWith("set") && md.method.parameterCount == 1
294+
}
295+
287296
internal fun Class<*>.findPublicSetterGetterIfHasPublicGetter(field: Field, packageName: String?): PublicSetterGetter? {
288297
@Suppress("DEPRECATION") val postfixName = field.name.capitalize()
289298
val setterName = "set$postfixName"

utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/SpringBeanValueProvider.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package org.utbot.fuzzing.spring
22

33
import org.utbot.framework.plugin.api.*
44
import org.utbot.framework.plugin.api.util.SpringModelUtils
5+
import org.utbot.framework.plugin.api.util.executableId
56
import org.utbot.framework.plugin.api.util.jClass
67
import org.utbot.fuzzer.FuzzedType
78
import org.utbot.fuzzer.FuzzedValue
89
import org.utbot.fuzzer.IdGenerator
910
import org.utbot.fuzzer.fuzzed
1011
import org.utbot.fuzzing.*
1112
import org.utbot.fuzzing.providers.SPRING_BEAN_PROP
13+
import org.utbot.fuzzing.providers.findAllPublicMethods
1214
import org.utbot.fuzzing.providers.nullRoutine
15+
import org.utbot.fuzzing.providers.removeSetters
1316

1417
class SpringBeanValueProvider(
1518
private val idGenerator: IdGenerator<Int>,
@@ -59,6 +62,19 @@ class SpringBeanValueProvider(
5962
)
6063
})
6164
}
65+
findAllPublicMethods(description, type.classId, description.description.packageName)
66+
.removeSetters()
67+
.forEach { md ->
68+
yield(Routine.Call(md.parameterTypes) { self, values ->
69+
val model = self.model as UtAssembleModel
70+
model.modificationsChain as MutableList +=
71+
UtExecutableCallModel(
72+
model,
73+
md.method.executableId,
74+
values.map { it.model }
75+
)
76+
})
77+
}
6278
},
6379
empty = nullRoutine(type.classId)
6480
)

0 commit comments

Comments
 (0)