Skip to content

Commit a696c82

Browse files
committed
Apply fixes under review
1 parent 5895f38 commit a696c82

File tree

3 files changed

+54
-29
lines changed
  • utbot-framework/src/main/kotlin/org/utbot/engine
  • utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api

3 files changed

+54
-29
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,13 @@ class WildcardTypeParameter : TypeParameters(emptyList())
11571157
open class StandardApplicationContext(
11581158
val mockFrameworkInstalled: Boolean = true,
11591159
val staticsMockingIsConfigured: Boolean = true,
1160-
)
1160+
) {
1161+
init {
1162+
if (!mockFrameworkInstalled) {
1163+
require(!staticsMockingIsConfigured) { "Static mocking cannot be used without mock framework" }
1164+
}
1165+
}
1166+
}
11611167

11621168
/**
11631169
* Data we get from Spring application context

utbot-framework/src/main/kotlin/org/utbot/engine/Mocks.kt

+31-22
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,30 @@ data class UtStaticMethodMockInfo(
132132
/**
133133
* A wrapper for [ObjectValue] to store additional onfo.
134134
*/
135-
sealed class MockedObjectInfo(val value: ObjectValue?)
135+
sealed class MockedObjectInfo {
136+
abstract val value: ObjectValue?
137+
}
136138

137-
object NoMock: MockedObjectInfo(value = null)
139+
object NoMock: MockedObjectInfo() {
140+
override val value: ObjectValue? = null
141+
}
138142

139143
/**
140144
* Represents a mock that occurs when mock strategy allows it
141145
* or when an object type is in a set that requires force mocking.
142146
*/
143-
class ExpectedMock(value: ObjectValue): MockedObjectInfo(value)
147+
class ExpectedMock(objectValue: ObjectValue): MockedObjectInfo() {
148+
override val value: ObjectValue = objectValue
149+
}
144150

145151
/**
146152
* Represents a mock that occurs when it is not recommended
147-
* E.g. mock strategy recommends do not mock
153+
* E.g. mock framework is not installed or
154+
* mock startegy is DO_NOT_MOCK and class is not in mockAlways set.
148155
*/
149-
class UnexpectedMock(value: ObjectValue): MockedObjectInfo(value)
156+
class UnexpectedMock(objectValue: ObjectValue): MockedObjectInfo() {
157+
override val value: ObjectValue = objectValue
158+
}
150159

151160
/**
152161
* Service to mock things. Knows mock strategy, class under test and class hierarchy.
@@ -166,24 +175,24 @@ class Mocker(
166175
* an information if this mock is expected or not.
167176
*/
168177
fun construct(value: ObjectValue?, mockInfo: UtMockInfo): MockedObjectInfo {
169-
return value
170-
?.let {
171-
val mockingIsPossible = when (mockInfo) {
172-
is UtFieldMockInfo,
173-
is UtObjectMockInfo -> applicationContext.mockFrameworkInstalled
174-
is UtNewInstanceMockInfo,
175-
is UtStaticMethodMockInfo,
176-
is UtStaticObjectMockInfo -> applicationContext.staticsMockingIsConfigured
177-
}
178-
val mockingIsForcedAndPossible = mockAlways(it.type) && mockingIsPossible
178+
if (value == null) {
179+
return NoMock
180+
}
179181

180-
if (mocksAreDesired || mockingIsForcedAndPossible) {
181-
ExpectedMock(it)
182-
} else {
183-
UnexpectedMock(it)
184-
}
185-
}
186-
?: NoMock
182+
val mockingIsPossible = when (mockInfo) {
183+
is UtFieldMockInfo,
184+
is UtObjectMockInfo -> applicationContext.mockFrameworkInstalled
185+
is UtNewInstanceMockInfo,
186+
is UtStaticMethodMockInfo,
187+
is UtStaticObjectMockInfo -> applicationContext.staticsMockingIsConfigured
188+
}
189+
val mockingIsForcedAndPossible = mockAlways(value.type) && mockingIsPossible
190+
191+
return if (mocksAreDesired || mockingIsForcedAndPossible) {
192+
ExpectedMock(value)
193+
} else {
194+
UnexpectedMock(value)
195+
}
187196
}
188197

189198
/**

utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt

+16-6
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,9 @@ class Traverser(
13841384

13851385
val mockedObject = mockedObjectInfo.value
13861386
if (mockedObjectInfo is UnexpectedMock) {
1387+
// if mock occurs, but it is unexpected due to some reasons
1388+
// (e.g. we do not have mock framework installed),
1389+
// we can only generate a test that uses null value for mocked object
13871390
queuedSymbolicStateUpdates += nullEqualityConstraint.asHardConstraint()
13881391
}
13891392

@@ -1474,10 +1477,17 @@ class Traverser(
14741477
val mockInfo = mockInfoGenerator.generate(addr)
14751478
val mockedObjectInfo = mocker.forceMock(type, mockInfoGenerator.generate(addr))
14761479

1477-
val mockedObject = mockedObjectInfo.value ?: error("Mocked value cannot be null after force mock")
1478-
if (mockedObjectInfo is UnexpectedMock) {
1479-
queuedSymbolicStateUpdates += nullEqualityConstraint.asHardConstraint()
1480-
return mockedObject
1480+
val mockedObject: ObjectValue = when (mockedObjectInfo) {
1481+
is NoMock -> error("Value must be mocked after the fore mock")
1482+
is ExpectedMock -> mockedObjectInfo.value
1483+
is UnexpectedMock -> {
1484+
// if mock occurs, but it is unexpected due to some reasons
1485+
// (e.g. we do not have mock framework installed),
1486+
// we can only generate a test that uses null value for mocked object
1487+
queuedSymbolicStateUpdates += nullEqualityConstraint.asHardConstraint()
1488+
1489+
mockedObjectInfo.value
1490+
}
14811491
}
14821492

14831493
queuedSymbolicStateUpdates += MemoryUpdate(mockInfos = persistentListOf(MockInfoEnriched(mockInfo)))
@@ -2680,7 +2690,7 @@ class Traverser(
26802690
} ?: findMethodInvocationTargets(types, methodSubSignature)
26812691

26822692
return methodInvocationTargets
2683-
.mapNotNull { (method, implementationClass, possibleTypes) ->
2693+
.map { (method, implementationClass, possibleTypes) ->
26842694
val typeStorage = typeResolver.constructTypeStorage(implementationClass, possibleTypes)
26852695
val mockInfo = memory.mockInfoByAddr(instance.addr)
26862696
val mockedObjectInfo = mockInfo?.let {
@@ -2710,7 +2720,7 @@ class Traverser(
27102720
InvocationTarget(wrapperOrInstance, method, constraints)
27112721
}
27122722
is ExpectedMock -> {
2713-
val mockedObject = mockedObjectInfo.value!!
2723+
val mockedObject = mockedObjectInfo.value
27142724
val typeConstraint = typeRegistry.typeConstraint(mockedObject.addr, mockedObject.typeStorage)
27152725
val constraints = setOf(typeConstraint.isOrNullConstraint())
27162726

0 commit comments

Comments
 (0)