Skip to content

Commit 7405050

Browse files
committed
Configure Spring unit tests to use fuzzer with mocks
1 parent 91c604b commit 7405050

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.utbot.framework.context.custom
2+
3+
import org.utbot.framework.context.JavaFuzzingContext
4+
import org.utbot.fuzzing.JavaValueProvider
5+
import org.utbot.fuzzing.providers.MapValueProvider
6+
import org.utbot.fuzzing.spring.unit.MockValueProvider
7+
import org.utbot.fuzzing.providers.NullValueProvider
8+
import org.utbot.fuzzing.providers.ObjectValueProvider
9+
import org.utbot.fuzzing.providers.StringValueProvider
10+
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult
11+
12+
/**
13+
* Makes fuzzer mock all types that don't have *specific* [JavaValueProvider],
14+
* like [MapValueProvider] or [StringValueProvider].
15+
*
16+
* NOTE: the caller is responsible for providing some *specific* [JavaValueProvider]
17+
* that can create values for class under test (otherwise it will be mocked),
18+
* [ObjectValueProvider] and [NullValueProvider] do not count as *specific*.
19+
*/
20+
fun JavaFuzzingContext.mockAllTypesWithoutSpecificValueProvider() =
21+
MockingJavaFuzzingContext(delegateContext = this)
22+
23+
class MockingJavaFuzzingContext(
24+
val delegateContext: JavaFuzzingContext
25+
) : JavaFuzzingContext by delegateContext {
26+
private val mockValueProvider = MockValueProvider(delegateContext.idGenerator)
27+
28+
override val valueProvider: JavaValueProvider =
29+
// NOTE: we first remove `NullValueProvider` from `delegateContext.valueProvider` and then
30+
// add it back as a part of our `withFallback` so it has the same priority as
31+
// `mockValueProvider`, otherwise mocks will never be used where `null` can be used.
32+
delegateContext.valueProvider
33+
.except { it is NullValueProvider }
34+
.except { it is ObjectValueProvider }
35+
.withFallback(
36+
mockValueProvider
37+
.with(NullValueProvider)
38+
)
39+
40+
override fun handleFuzzedConcreteExecutionResult(concreteExecutionResult: UtConcreteExecutionResult) =
41+
mockValueProvider.addMockingCandidates(concreteExecutionResult.detectedMockingCandidates)
42+
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import org.utbot.framework.context.ConcreteExecutionContext
1111
import org.utbot.framework.context.NonNullSpeculator
1212
import org.utbot.framework.context.TypeReplacer
1313
import org.utbot.framework.context.custom.CoverageFilteringConcreteExecutionContext
14+
import org.utbot.framework.context.custom.mockAllTypesWithoutSpecificValueProvider
15+
import org.utbot.framework.context.utils.transformJavaFuzzingContext
16+
import org.utbot.framework.context.utils.withValueProvider
1417
import org.utbot.framework.plugin.api.BeanDefinitionData
1518
import org.utbot.framework.plugin.api.ClassId
1619
import org.utbot.framework.plugin.api.ConcreteContextLoadingResult
@@ -21,6 +24,7 @@ import org.utbot.framework.plugin.api.util.allSuperTypes
2124
import org.utbot.framework.plugin.api.util.id
2225
import org.utbot.framework.plugin.api.util.jClass
2326
import org.utbot.framework.plugin.api.util.utContext
27+
import org.utbot.fuzzing.spring.unit.InjectMockValueProvider
2428

2529
class SpringApplicationContextImpl(
2630
private val delegateContext: ApplicationContext,
@@ -58,7 +62,16 @@ class SpringApplicationContextImpl(
5862
)
5963

6064
return when (springTestType) {
61-
SpringTestType.UNIT_TEST -> delegateConcreteExecutionContext
65+
SpringTestType.UNIT_TEST -> delegateConcreteExecutionContext.transformJavaFuzzingContext { fuzzingContext ->
66+
fuzzingContext
67+
.withValueProvider(
68+
InjectMockValueProvider(
69+
idGenerator = fuzzingContext.idGenerator,
70+
classToUseCompositeModelFor = fuzzingContext.classUnderTest
71+
)
72+
)
73+
.mockAllTypesWithoutSpecificValueProvider()
74+
}
6275
SpringTestType.INTEGRATION_TEST -> SpringIntegrationTestConcreteExecutionContext(
6376
delegateConcreteExecutionContext,
6477
classpathWithoutDependencies,

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/UtTestsDialogProcessor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ object UtTestsDialogProcessor {
387387
}
388388
val useFuzzing = when (model.projectType) {
389389
Spring -> when (model.springTestType) {
390-
UNIT_TEST -> false
390+
UNIT_TEST -> UtSettings.useFuzzing
391391
INTEGRATION_TEST -> true
392392
}
393393

0 commit comments

Comments
 (0)