Skip to content

Commit 1ed40b5

Browse files
committed
Disallow non-mocks when mock can be used
1 parent 0255f6d commit 1ed40b5

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
@@ -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-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)