From 5f114cea15fc30aa3e2f16f946de42b7b5f71f24 Mon Sep 17 00:00:00 2001 From: IlyaMuravjov Date: Tue, 29 Aug 2023 13:50:43 +0300 Subject: [PATCH] Only use `InjectMockValueProvider` for `thisInstance` --- .../spring/SpringApplicationContextImpl.kt | 2 +- .../utbot/fuzzing/spring/unit/InjectMocks.kt | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringApplicationContextImpl.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringApplicationContextImpl.kt index 88d5eec0ab..746fb6c706 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringApplicationContextImpl.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringApplicationContextImpl.kt @@ -74,7 +74,7 @@ class SpringApplicationContextImpl( .transformValueProvider { origValueProvider -> InjectMockValueProvider( idGenerator = fuzzingContext.idGenerator, - classToUseCompositeModelFor = fuzzingContext.classUnderTest + classUnderTest = fuzzingContext.classUnderTest ) .withFallback(origValueProvider) .replaceTypes { description, type -> diff --git a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/unit/InjectMocks.kt b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/unit/InjectMocks.kt index d6bb17f8b8..3843930e92 100644 --- a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/unit/InjectMocks.kt +++ b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/unit/InjectMocks.kt @@ -13,20 +13,36 @@ import org.utbot.fuzzer.fuzzed import org.utbot.fuzzing.FuzzedDescription import org.utbot.fuzzing.JavaValueProvider import org.utbot.fuzzing.Routine +import org.utbot.fuzzing.Scope +import org.utbot.fuzzing.ScopeProperty import org.utbot.fuzzing.Seed import org.utbot.fuzzing.toFuzzerType +val INJECT_MOCK_FLAG = ScopeProperty( + "INJECT_MOCK_FLAG is present if composite model should be used (i.e. thisInstance is being created)" +) + /** * Models created by this class can be used with `@InjectMock` annotation, because * they are [UtCompositeModel]s similar to the ones created by the symbolic engine. + * + * This class only creates models for thisInstance of type [classUnderTest]. */ class InjectMockValueProvider( private val idGenerator: IdGenerator, - private val classToUseCompositeModelFor: ClassId + private val classUnderTest: ClassId ) : JavaValueProvider { - override fun accept(type: FuzzedType): Boolean = type.classId == classToUseCompositeModelFor + override fun enrich(description: FuzzedDescription, type: FuzzedType, scope: Scope) { + // any value except this + if (description.description.isStatic == false && scope.parameterIndex == 0 && scope.recursionDepth == 1) { + scope.putProperty(INJECT_MOCK_FLAG, Unit) + } + } + + override fun accept(type: FuzzedType): Boolean = type.classId == classUnderTest override fun generate(description: FuzzedDescription, type: FuzzedType): Sequence> { + if (description.scope?.getProperty(INJECT_MOCK_FLAG) == null) return emptySequence() val fields = type.classId.allDeclaredFieldIds.filterNot { it.isStatic && it.isFinal }.toList() return sequenceOf(Seed.Recursive( construct = Routine.Create(types = fields.map { toFuzzerType(it.jField.genericType, description.typeCache) }) { values ->