Skip to content

Commit 163e168

Browse files
committed
Disallow non-mocks when mock can be used
1 parent 5f114ce commit 163e168

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

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utbot.framework.context.custom
22

33
import org.utbot.framework.context.JavaFuzzingContext
4+
import org.utbot.fuzzer.FuzzedType
45
import org.utbot.fuzzing.JavaValueProvider
56
import org.utbot.fuzzing.providers.AnyDepthNullValueProvider
67
import org.utbot.fuzzing.providers.MapValueProvider
@@ -13,17 +14,19 @@ import org.utbot.fuzzing.spring.decorators.filterTypes
1314
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult
1415

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

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

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

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.utbot.framework.context.ConcreteExecutionContext
1313
import org.utbot.framework.context.NonNullSpeculator
1414
import org.utbot.framework.context.TypeReplacer
1515
import org.utbot.framework.context.custom.CoverageFilteringConcreteExecutionContext
16-
import org.utbot.framework.context.custom.allowMocks
16+
import org.utbot.framework.context.custom.useMocks
1717
import org.utbot.framework.context.utils.transformJavaFuzzingContext
1818
import org.utbot.framework.context.utils.transformValueProvider
1919
import org.utbot.framework.plugin.api.BeanDefinitionData
@@ -26,10 +26,11 @@ import org.utbot.framework.plugin.api.util.allSuperTypes
2626
import org.utbot.framework.plugin.api.util.id
2727
import org.utbot.framework.plugin.api.util.jClass
2828
import org.utbot.framework.plugin.api.util.utContext
29+
import org.utbot.fuzzing.spring.FuzzedTypeFlag
2930
import org.utbot.fuzzing.spring.addProperties
3031
import org.utbot.fuzzing.spring.decorators.replaceTypes
32+
import org.utbot.fuzzing.spring.properties
3133
import org.utbot.fuzzing.spring.unit.InjectMockValueProvider
32-
import org.utbot.fuzzing.spring.unit.NeverMockFlag
3334
import org.utbot.fuzzing.toFuzzerType
3435

3536
class SpringApplicationContextImpl(
@@ -42,6 +43,8 @@ class SpringApplicationContextImpl(
4243
private val logger = KotlinLogging.logger {}
4344
}
4445

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

@@ -70,7 +73,13 @@ class SpringApplicationContextImpl(
7073
return when (springTestType) {
7174
SpringTestType.UNIT_TEST -> delegateConcreteExecutionContext.transformJavaFuzzingContext { fuzzingContext ->
7275
fuzzingContext
73-
.allowMocks()
76+
.useMocks { type ->
77+
ReplacedFuzzedTypeFlag !in type.properties &&
78+
fuzzingContext.mockStrategy.eligibleToMock(
79+
classToMock = type.classId,
80+
classUnderTest = fuzzingContext.classUnderTest
81+
)
82+
}
7483
.transformValueProvider { origValueProvider ->
7584
InjectMockValueProvider(
7685
idGenerator = fuzzingContext.idGenerator,
@@ -83,7 +92,7 @@ class SpringApplicationContextImpl(
8392
?.let { replacement ->
8493
// TODO infer generic type of replacement
8594
toFuzzerType(replacement.jClass, description.typeCache).addProperties(
86-
dynamicPropertiesOf(NeverMockFlag.withValue(Unit))
95+
dynamicPropertiesOf(ReplacedFuzzedTypeFlag.withValue(Unit))
8796
)
8897
} ?: type
8998
}

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

-6
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 {

0 commit comments

Comments
 (0)