Skip to content

Commit c21a916

Browse files
authored
Use controllers via MockMvc in Spring integration tests (#2447)
* Use controllers via `MockMvc` in concrete execution and update result models * Make `createGetMockMvcResponseModel` work when there's no `@RequestMapping` on controller class * Introduce `executableToCall` * Replace `executableToCall` with `MockMvc.perform` for Spring controllers * Remove handling of Spring controllers from `SpringUtExecutionInstrumentation` * Add `utCustomModelConstructorFinder` property to `UtModelConstructor` * Add `UtSpringMockMvcResultActionsModel` and its `UtCustomModelConstructor` * Add `UtCustomModel` (i.e. common parent of all framework specific models) * Properly handle `UtCustomModel` * Add `origin` to `UtSpringMockMvcResultActionsModel` * Split `CgMethodTestSet.executableId` into `executableUnderTest` and `executablesToCall` * Enable `UtMockMvcResultActionsModelConstructor` for subtypes of `ResultActions` * Add `CgCustomAssertConstructor` to `CgComponents` * Add custom asserts for `MockMvc.perform()` result * Remove redundant code * Handle `modelTagName` for `UtCustomModel` * Fix compilation after rebase * Fix JS compilation * Address comments in #2447
1 parent 65683b0 commit c21a916

File tree

58 files changed

+1310
-374
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1310
-374
lines changed

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

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ abstract class UtExecution(
142142
testMethodName: String? = this.testMethodName,
143143
displayName: String? = this.displayName,
144144
): UtExecution
145+
146+
val executableToCall get() = stateBefore.executableToCall
145147
}
146148

147149
/**
@@ -287,11 +289,33 @@ class UtFailedExecution(
287289
}
288290
}
289291

292+
/**
293+
* @property executableToCall executable that is called in the test body and whose result is used in asserts as `actual`.
294+
*
295+
* Most often [executableToCall] is just method under test, but it may be changed to different executable if more
296+
* appropriate way of calling specific method is found (for example, Spring controller methods are called via `MockMvc`).
297+
*
298+
* `null` value of [executableToCall] indicates that method under test should be called in the test body.
299+
*/
290300
open class EnvironmentModels(
291301
val thisInstance: UtModel?,
292302
val parameters: List<UtModel>,
293-
val statics: Map<FieldId, UtModel>
303+
val statics: Map<FieldId, UtModel>,
304+
val executableToCall: ExecutableId?,
294305
) {
306+
@Deprecated("Now `executableToCall` also need to be passed to `EnvironmentModels` constructor " +
307+
"(see more details in `EnvironmentModels` class documentation)", level = DeprecationLevel.ERROR)
308+
constructor(
309+
thisInstance: UtModel?,
310+
parameters: List<UtModel>,
311+
statics: Map<FieldId, UtModel>,
312+
) : this(
313+
thisInstance = thisInstance,
314+
parameters = parameters,
315+
statics = statics,
316+
executableToCall = null,
317+
)
318+
295319
override fun toString() = buildString {
296320
append("this=$thisInstance")
297321
appendOptional("parameters", parameters)
@@ -305,8 +329,9 @@ open class EnvironmentModels(
305329
fun copy(
306330
thisInstance: UtModel? = this.thisInstance,
307331
parameters: List<UtModel> = this.parameters,
308-
statics: Map<FieldId, UtModel> = this.statics
309-
) = EnvironmentModels(thisInstance, parameters, statics)
332+
statics: Map<FieldId, UtModel> = this.statics,
333+
executableToCall: ExecutableId? = this.executableToCall,
334+
) = EnvironmentModels(thisInstance, parameters, statics, executableToCall)
310335
}
311336

312337
/**
@@ -315,7 +340,8 @@ open class EnvironmentModels(
315340
object MissingState : EnvironmentModels(
316341
thisInstance = null,
317342
parameters = emptyList(),
318-
statics = emptyMap()
343+
statics = emptyMap(),
344+
executableToCall = null,
319345
)
320346

321347
/**
@@ -550,6 +576,21 @@ data class UtArrayModel(
550576
override fun hashCode(): Int = id
551577
}
552578

579+
/**
580+
* Wrapper of [origin] model, that can be handled in a different
581+
* way in some situations (e.g. during value construction).
582+
*/
583+
sealed class UtModelWithCompositeOrigin(
584+
id: Int?,
585+
classId: ClassId,
586+
modelName: String = id.toString(),
587+
open val origin: UtCompositeModel?,
588+
) : UtReferenceModel(
589+
id = id,
590+
classId = classId,
591+
modelName = modelName
592+
)
593+
553594
/**
554595
* Model for complex objects with assemble instructions.
555596
*
@@ -564,8 +605,8 @@ data class UtAssembleModel private constructor(
564605
override val modelName: String,
565606
val instantiationCall: UtStatementCallModel,
566607
val modificationsChain: List<UtStatementModel>,
567-
val origin: UtCompositeModel?
568-
) : UtReferenceModel(id, classId, modelName) {
608+
override val origin: UtCompositeModel?
609+
) : UtModelWithCompositeOrigin(id, classId, modelName, origin) {
569610

570611
/**
571612
* Creates a new [UtAssembleModel].
@@ -703,7 +744,17 @@ class UtLambdaModel(
703744
}
704745
}
705746

706-
object UtSpringContextModel : UtReferenceModel(
747+
/**
748+
* Common parent of all framework-specific models (e.g. Spring-specific models)
749+
*/
750+
abstract class UtCustomModel(
751+
id: Int?,
752+
classId: ClassId,
753+
modelName: String = id.toString(),
754+
override val origin: UtCompositeModel? = null,
755+
) : UtModelWithCompositeOrigin(id, classId, modelName, origin)
756+
757+
object UtSpringContextModel : UtCustomModel(
707758
id = null,
708759
classId = SpringModelUtils.applicationContextClassId,
709760
modelName = "applicationContext"
@@ -721,6 +772,23 @@ data class SpringRepositoryId(
721772
val entityClassId: ClassId,
722773
)
723774

775+
class UtSpringMockMvcResultActionsModel(
776+
id: Int?,
777+
origin: UtCompositeModel?,
778+
val status: Int,
779+
val errorMessage: String?,
780+
val contentAsString: String,
781+
val viewName: String?,
782+
// model for mvcResult.modelAndView?.model
783+
val model: UtModel?
784+
// TODO add headers and other data
785+
) : UtCustomModel(
786+
origin = origin,
787+
classId = SpringModelUtils.resultActionsClassId,
788+
id = id,
789+
modelName = "mockMvcResultActions@$id"
790+
)
791+
724792
/**
725793
* Model for a step to obtain [UtAssembleModel].
726794
*/

0 commit comments

Comments
 (0)