11package org.utbot.fuzzing.spring.unit
22
3+ import com.google.common.reflect.TypeResolver
34import mu.KotlinLogging
45import org.utbot.framework.plugin.api.ClassId
56import org.utbot.framework.plugin.api.MethodId
@@ -17,6 +18,9 @@ import org.utbot.fuzzing.Routine
1718import org.utbot.fuzzing.Scope
1819import org.utbot.fuzzing.ScopeProperty
1920import org.utbot.fuzzing.Seed
21+ import org.utbot.fuzzing.spring.utils.jType
22+ import org.utbot.fuzzing.spring.utils.toTypeParametrizedByTypeVariables
23+ import org.utbot.fuzzing.spring.utils.typeToken
2024import org.utbot.fuzzing.toFuzzerType
2125
2226val methodsToMockProperty = ScopeProperty <Set <MethodId >>(
@@ -28,6 +32,7 @@ class MockValueProvider(private val idGenerator: IdGenerator<Int>) : JavaValuePr
2832 companion object {
2933 private val logger = KotlinLogging .logger {}
3034 private val loggedMockedMethods = mutableSetOf<MethodId >()
35+ private val loggedUnresolvedMethods = mutableSetOf<MethodId >()
3136 }
3237
3338 private val methodsToMock = mutableSetOf<MethodId >()
@@ -44,12 +49,26 @@ class MockValueProvider(private val idGenerator: IdGenerator<Int>) : JavaValuePr
4449 construct = Routine .Create (types = emptyList()) { emptyMockFuzzedValue(type.classId) },
4550 empty = Routine .Empty { emptyMockFuzzedValue(type.classId) },
4651 modify = (description.scope?.getProperty(methodsToMockProperty)?.asSequence() ? : emptySequence()).map { methodId ->
47- if (loggedMockedMethods.add(methodId))
48- logger.info { " Actually mocked $methodId for the first time" }
49- // TODO accept `List<returnType>` instead of singular `returnType`
50- Routine .Call (types = listOf (
51- toFuzzerType(methodId.method.genericReturnType, description.typeCache)
52- )) { instance, (value) ->
52+ val methodDeclaringClass = methodId.classId.jClass
53+
54+ val returnType = try {
55+ TypeResolver ().where(
56+ methodDeclaringClass.toTypeParametrizedByTypeVariables(),
57+ @Suppress(" UNCHECKED_CAST" )
58+ type.jType.typeToken.getSupertype(methodDeclaringClass as Class <in Any >).type
59+ ).resolveType(methodId.method.genericReturnType)
60+ } catch (e: Exception ) {
61+ if (loggedUnresolvedMethods.add(methodId))
62+ logger.error(e) { " Failed to resolve return type for $methodId , using unresolved generic type" }
63+
64+ methodId.method.genericReturnType
65+ }
66+
67+ // TODO accept `List<resolvedReturnType>` instead of singular `resolvedReturnType`
68+ Routine .Call (types = listOf (toFuzzerType(returnType, description.typeCache))) { instance, (value) ->
69+ if (loggedMockedMethods.add(methodId))
70+ logger.info { " Actually mocked $methodId for the first time" }
71+
5372 (instance.model as UtCompositeModel ).mocks[methodId] = listOf (value.model)
5473 }
5574 }
0 commit comments