Skip to content

Commit 047a3f8

Browse files
committed
Avoid mocking replaced types
1 parent b5e6527 commit 047a3f8

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.utbot.framework.context.spring
22

33
import mu.KotlinLogging
4+
import org.utbot.common.dynamicPropertiesOf
45
import org.utbot.common.isAbstract
56
import org.utbot.common.isStatic
7+
import org.utbot.common.withValue
68
import org.utbot.framework.codegen.generator.AbstractCodeGenerator
79
import org.utbot.framework.codegen.generator.CodeGeneratorParams
810
import org.utbot.framework.codegen.generator.SpringCodeGenerator
@@ -24,8 +26,10 @@ import org.utbot.framework.plugin.api.util.allSuperTypes
2426
import org.utbot.framework.plugin.api.util.id
2527
import org.utbot.framework.plugin.api.util.jClass
2628
import org.utbot.framework.plugin.api.util.utContext
29+
import org.utbot.fuzzing.spring.addProperties
2730
import org.utbot.fuzzing.spring.decorators.replaceTypes
2831
import org.utbot.fuzzing.spring.unit.InjectMockValueProvider
32+
import org.utbot.fuzzing.spring.unit.NeverMockFlag
2933
import org.utbot.fuzzing.toFuzzerType
3034

3135
class SpringApplicationContextImpl(
@@ -74,10 +78,14 @@ class SpringApplicationContextImpl(
7478
)
7579
.withFallback(origValueProvider)
7680
.replaceTypes { description, type ->
77-
typeReplacer.replaceTypeIfNeeded(type.classId)?.let { replacement ->
78-
// TODO infer generic type
79-
toFuzzerType(replacement.jClass, description.typeCache)
80-
} ?: type
81+
typeReplacer.replaceTypeIfNeeded(type.classId)
82+
?.takeIf { it != type.classId }
83+
?.let { replacement ->
84+
// TODO infer generic type of replacement
85+
toFuzzerType(replacement.jClass, description.typeCache).addProperties(
86+
dynamicPropertiesOf(NeverMockFlag.withValue(Unit))
87+
)
88+
} ?: type
8189
}
8290
}
8391
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ import org.utbot.fuzzing.Routine
1818
import org.utbot.fuzzing.Scope
1919
import org.utbot.fuzzing.ScopeProperty
2020
import org.utbot.fuzzing.Seed
21+
import org.utbot.fuzzing.spring.FuzzedTypeFlag
22+
import org.utbot.fuzzing.spring.properties
2123
import org.utbot.fuzzing.spring.utils.jType
2224
import org.utbot.fuzzing.spring.utils.toTypeParametrizedByTypeVariables
2325
import org.utbot.fuzzing.spring.utils.typeToken
2426
import org.utbot.fuzzing.toFuzzerType
2527

28+
object NeverMockFlag : FuzzedTypeFlag
29+
2630
val methodsToMockProperty = ScopeProperty<Set<MethodId>>(
2731
description = "Method ids that can be mocked by `MockValueProvider`"
2832
)
@@ -37,6 +41,8 @@ class MockValueProvider(private val idGenerator: IdGenerator<Int>) : JavaValuePr
3741

3842
private val methodsToMock = mutableSetOf<MethodId>()
3943

44+
override fun accept(type: FuzzedType) = NeverMockFlag !in type.properties
45+
4046
override fun enrich(description: FuzzedDescription, type: FuzzedType, scope: Scope) {
4147
val publicMethods = type.classId.jClass.methods.map { it.executableId }
4248
publicMethods.intersect(methodsToMock).takeIf { it.isNotEmpty() }?.let {

0 commit comments

Comments
 (0)