Skip to content

Commit e0ad607

Browse files
committed
Be able to invoke all public methods in integration tests
1 parent 3954ddb commit e0ad607

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
@@ -86,7 +86,7 @@ class SpringIntegrationTestConcreteExecutionContext(
8686
springApplicationContext.getBeansAssignableTo(classId).map { it.beanName }
8787
},
8888
relevantRepositories = relevantRepositories
89-
).withFallback(anyObjectValueProvider(idGenerator))
89+
).withFallback(anyObjectValueProvider(idGenerator, isSpringProject = true))
9090

9191
return delegateContext.tryCreateValueProvider(concreteExecutor, classUnderTest, idGenerator)
9292
.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)
@@ -213,7 +216,7 @@ internal class FieldDescription(
213216
internal class MethodDescription(
214217
val name: String,
215218
val parameterTypes: List<FuzzedType>,
216-
val executable: ExecutableId
219+
val method: Method
217220
)
218221

219222
internal fun findAccessibleModifiableFields(description: FuzzedDescription?, classId: ClassId, packageName: String?): List<FieldDescription> {
@@ -244,6 +247,7 @@ internal fun findAllPublicMethods(
244247
): List<MethodDescription> =
245248
classId.jClass.declaredMethods.mapNotNull { method ->
246249
if (isAccessible(method, packageName)) {
250+
if (method.genericParameterTypes.any { it !is Class<*> }) return@mapNotNull null
247251
val parameterTypes =
248252
method
249253
.parameterTypes
@@ -259,11 +263,16 @@ internal fun findAllPublicMethods(
259263
MethodDescription(
260264
name = method.name,
261265
parameterTypes = parameterTypes,
262-
executable = method.executableId
266+
method = method
263267
)
264268
} else null
265269
}
266270

271+
internal fun List<MethodDescription>.removeSetters(): List<MethodDescription> =
272+
filterNot { md ->
273+
md.method.name.startsWith("set") && md.method.parameterCount == 1
274+
}
275+
267276
internal fun Class<*>.findPublicSetterGetterIfHasPublicGetter(field: Field, packageName: String?): PublicSetterGetter? {
268277
@Suppress("DEPRECATION") val postfixName = field.name.capitalize()
269278
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)