Skip to content

Commit 5f114ce

Browse files
committed
Only use InjectMockValueProvider for thisInstance
1 parent 047a3f8 commit 5f114ce

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class SpringApplicationContextImpl(
7474
.transformValueProvider { origValueProvider ->
7575
InjectMockValueProvider(
7676
idGenerator = fuzzingContext.idGenerator,
77-
classToUseCompositeModelFor = fuzzingContext.classUnderTest
77+
classUnderTest = fuzzingContext.classUnderTest
7878
)
7979
.withFallback(origValueProvider)
8080
.replaceTypes { description, type ->

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,36 @@ import org.utbot.fuzzer.fuzzed
1313
import org.utbot.fuzzing.FuzzedDescription
1414
import org.utbot.fuzzing.JavaValueProvider
1515
import org.utbot.fuzzing.Routine
16+
import org.utbot.fuzzing.Scope
17+
import org.utbot.fuzzing.ScopeProperty
1618
import org.utbot.fuzzing.Seed
1719
import org.utbot.fuzzing.toFuzzerType
1820

21+
val INJECT_MOCK_FLAG = ScopeProperty<Unit>(
22+
"INJECT_MOCK_FLAG is present if composite model should be used (i.e. thisInstance is being created)"
23+
)
24+
1925
/**
2026
* Models created by this class can be used with `@InjectMock` annotation, because
2127
* they are [UtCompositeModel]s similar to the ones created by the symbolic engine.
28+
*
29+
* This class only creates models for thisInstance of type [classUnderTest].
2230
*/
2331
class InjectMockValueProvider(
2432
private val idGenerator: IdGenerator<Int>,
25-
private val classToUseCompositeModelFor: ClassId
33+
private val classUnderTest: ClassId
2634
) : JavaValueProvider {
27-
override fun accept(type: FuzzedType): Boolean = type.classId == classToUseCompositeModelFor
35+
override fun enrich(description: FuzzedDescription, type: FuzzedType, scope: Scope) {
36+
// any value except this
37+
if (description.description.isStatic == false && scope.parameterIndex == 0 && scope.recursionDepth == 1) {
38+
scope.putProperty(INJECT_MOCK_FLAG, Unit)
39+
}
40+
}
41+
42+
override fun accept(type: FuzzedType): Boolean = type.classId == classUnderTest
2843

2944
override fun generate(description: FuzzedDescription, type: FuzzedType): Sequence<Seed<FuzzedType, FuzzedValue>> {
45+
if (description.scope?.getProperty(INJECT_MOCK_FLAG) == null) return emptySequence()
3046
val fields = type.classId.allDeclaredFieldIds.filterNot { it.isStatic && it.isFinal }.toList()
3147
return sequenceOf(Seed.Recursive(
3248
construct = Routine.Create(types = fields.map { toFuzzerType(it.jField.genericType, description.typeCache) }) { values ->

0 commit comments

Comments
 (0)