Skip to content

Correct two regressions in Spring codegen #2247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ private abstract class StaticMocker(

private class MockitoMocker(context: CgContext) : ObjectMocker(context) {

private val alreadyMockedModels: MutableSet<UtCompositeModel> = Collections.newSetFromMap(IdentityHashMap())

override fun createMock(model: UtCompositeModel, baseName: String): CgVariable {
val modelClass = getClassOf(model.classId)
val mockObject = newVar(model.classId, baseName = baseName, isMock = true) { mock(modelClass) }
Expand All @@ -195,10 +193,6 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) {
}

fun mockForVariable(model: UtCompositeModel, mockObject: CgVariable) {
if (!alreadyMockedModels.add(model)) {
return
}

for ((executable, values) in model.mocks) {
val matchers = mockitoArgumentMatchersFor(executable)

Expand All @@ -225,7 +219,12 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) {
error("Cannot mock method $executable as it is not accessible from package $testClassPackageName")
}

val results = values.map { variableConstructor.getOrCreateVariable(it) }.toTypedArray()
val results = values
.map { value ->
// Sometimes we need mocks returning itself, e.g. for StringBuilder.append method
if (value != model) variableConstructor.getOrCreateVariable(value) else mockObject
}
.toTypedArray()
`when`(mockObject[executable](*matchers)).thenReturn(executable.returnType, *results)
}
else -> error("Only MethodId was expected to appear in simple mocker but got $executable")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,32 @@ import org.utbot.framework.plugin.api.util.objectClassId

class CgSpringUnitTestClassConstructor(context: CgContext) : CgAbstractSpringTestClassConstructor(context) {

private var additionalMethodsRequired: Boolean = false

private lateinit var mockitoCloseableVariable: CgValue

override fun constructClassFields(testClassModel: SpringTestClassModel): List<CgFieldDeclaration> {
val fields = mutableListOf<CgFieldDeclaration>()
val mockedFields = constructFieldsWithAnnotation(testClassModel.thisInstanceDependentMocks, mockClassId)

if (mockedFields.isNotEmpty()) {
fields += constructFieldsWithAnnotation(testClassModel.thisInstanceModels, injectMocksClassId)
fields += mockedFields
if (testClassModel.thisInstanceDependentMocks.isNotEmpty()) {
val mockedFields = constructFieldsWithAnnotation(testClassModel.thisInstanceDependentMocks, mockClassId)
val injectingMocksFields = constructFieldsWithAnnotation(testClassModel.thisInstanceModels, injectMocksClassId)

fields += injectingMocksFields
fields += mockedFields
fields += constructMockitoCloseables()

additionalMethodsRequired = true
}

return fields
}

override fun constructAdditionalMethods(): CgMethodsCluster {
if (!additionalMethodsRequired) {
return CgMethodsCluster(header = null, content = emptyList(),)
}

importIfNeeded(openMocksMethodId)

val openMocksCall = CgMethodCall(
Expand Down