Skip to content

Commit 3a15ae3

Browse files
committed
Disallow non-mocks when mock can be used
1 parent badfd3b commit 3a15ae3

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/context/custom/MockingJavaFuzzingContext.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.utbot.framework.context.custom
22

33
import org.utbot.framework.context.JavaFuzzingContext
44
import org.utbot.framework.plugin.api.ExecutableId
5+
import org.utbot.fuzzer.FuzzedType
56
import org.utbot.fuzzing.JavaValueProvider
67
import org.utbot.fuzzing.providers.AnyDepthNullValueProvider
78
import org.utbot.fuzzing.providers.MapValueProvider
@@ -14,17 +15,19 @@ import org.utbot.fuzzing.spring.decorators.filterTypes
1415
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult
1516

1617
/**
17-
* Allows fuzzer to use mocks in accordance with [JavaFuzzingContext.mockStrategy].
18+
* Makes fuzzer to use mocks in accordance with [mockPredicate].
1819
*
1920
* NOTE:
2021
* - fuzzer won't mock types, that have *specific* value providers (e.g. [MapValueProvider] and [StringValueProvider])
2122
* - [ObjectValueProvider] and [NullValueProvider] do not count as *specific* value providers
23+
* - fuzzer may still resort to mocks despite [mockPredicate] if it can't create other non-null values or at runtime
2224
*/
23-
fun JavaFuzzingContext.allowMocks() =
24-
MockingJavaFuzzingContext(delegateContext = this)
25+
fun JavaFuzzingContext.useMocks(mockPredicate: (FuzzedType) -> Boolean) =
26+
MockingJavaFuzzingContext(delegateContext = this, mockPredicate)
2527

2628
class MockingJavaFuzzingContext(
2729
val delegateContext: JavaFuzzingContext,
30+
val mockPredicate: (FuzzedType) -> Boolean,
2831
) : JavaFuzzingContext by delegateContext {
2932
private val mockValueProvider = MockValueProvider(delegateContext.idGenerator)
3033

@@ -36,14 +39,8 @@ class MockingJavaFuzzingContext(
3639
.except { it is NullValueProvider }
3740
.except { it is ObjectValueProvider }
3841
.withFallback(
39-
mockValueProvider
40-
.filterTypes { type ->
41-
mockStrategy.eligibleToMock(
42-
classToMock = type.classId,
43-
classUnderTest = classUnderTest
44-
)
45-
}
46-
.with(anyObjectValueProvider(idGenerator))
42+
mockValueProvider.filterTypes(mockPredicate)
43+
.with(anyObjectValueProvider(idGenerator).filterTypes { !mockPredicate(it) })
4744
.withFallback(mockValueProvider.with(AnyDepthNullValueProvider))
4845
.with(NullValueProvider)
4946
)

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@ 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
2321
import org.utbot.fuzzing.spring.utils.jType
2422
import org.utbot.fuzzing.spring.utils.toTypeParametrizedByTypeVariables
2523
import org.utbot.fuzzing.spring.utils.typeToken
2624
import org.utbot.fuzzing.toFuzzerType
2725

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

4238
private val methodsToMock = mutableSetOf<MethodId>()
4339

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

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.utbot.framework.context.NonNullSpeculator
1414
import org.utbot.framework.context.TypeReplacer
1515
import org.utbot.framework.context.custom.CoverageFilteringConcreteExecutionContext
1616
import org.utbot.framework.context.custom.RerunningConcreteExecutionContext
17-
import org.utbot.framework.context.custom.allowMocks
17+
import org.utbot.framework.context.custom.useMocks
1818
import org.utbot.framework.context.utils.transformJavaFuzzingContext
1919
import org.utbot.framework.context.utils.transformValueProvider
2020
import org.utbot.framework.plugin.api.BeanDefinitionData
@@ -27,10 +27,11 @@ import org.utbot.framework.plugin.api.util.allSuperTypes
2727
import org.utbot.framework.plugin.api.util.id
2828
import org.utbot.framework.plugin.api.util.jClass
2929
import org.utbot.framework.plugin.api.util.utContext
30+
import org.utbot.fuzzing.spring.FuzzedTypeFlag
3031
import org.utbot.fuzzing.spring.addProperties
3132
import org.utbot.fuzzing.spring.decorators.replaceTypes
33+
import org.utbot.fuzzing.spring.properties
3234
import org.utbot.fuzzing.spring.unit.InjectMockValueProvider
33-
import org.utbot.fuzzing.spring.unit.NeverMockFlag
3435
import org.utbot.fuzzing.toFuzzerType
3536

3637
class SpringApplicationContextImpl(
@@ -43,6 +44,8 @@ class SpringApplicationContextImpl(
4344
private val logger = KotlinLogging.logger {}
4445
}
4546

47+
private object ReplacedFuzzedTypeFlag : FuzzedTypeFlag
48+
4649
override val typeReplacer: TypeReplacer = SpringTypeReplacer(delegateContext.typeReplacer, this)
4750
override val nonNullSpeculator: NonNullSpeculator = SpringNonNullSpeculator(delegateContext.nonNullSpeculator, this)
4851

@@ -71,7 +74,13 @@ class SpringApplicationContextImpl(
7174
return when (springTestType) {
7275
SpringTestType.UNIT_TEST -> delegateConcreteExecutionContext.transformJavaFuzzingContext { fuzzingContext ->
7376
fuzzingContext
74-
.allowMocks()
77+
.useMocks { type ->
78+
ReplacedFuzzedTypeFlag !in type.properties &&
79+
fuzzingContext.mockStrategy.eligibleToMock(
80+
classToMock = type.classId,
81+
classUnderTest = fuzzingContext.classUnderTest
82+
)
83+
}
7584
.transformValueProvider { origValueProvider ->
7685
InjectMockValueProvider(
7786
idGenerator = fuzzingContext.idGenerator,
@@ -84,7 +93,7 @@ class SpringApplicationContextImpl(
8493
?.let { replacement ->
8594
// TODO infer generic type of replacement
8695
toFuzzerType(replacement.jClass, description.typeCache).addProperties(
87-
dynamicPropertiesOf(NeverMockFlag.withValue(Unit))
96+
dynamicPropertiesOf(ReplacedFuzzedTypeFlag.withValue(Unit))
8897
)
8998
} ?: type
9099
}

0 commit comments

Comments
 (0)