@@ -13,11 +13,13 @@ import org.utbot.framework.codegen.domain.builtin.anyLong
13
13
import org.utbot.framework.codegen.domain.builtin.anyOfClass
14
14
import org.utbot.framework.codegen.domain.builtin.anyShort
15
15
import org.utbot.framework.codegen.domain.builtin.argumentMatchersClassId
16
+ import org.utbot.framework.codegen.domain.builtin.doNothingMethodId
16
17
import org.utbot.framework.codegen.domain.builtin.mockMethodId
17
18
import org.utbot.framework.codegen.domain.builtin.mockedConstructionContextClassId
18
19
import org.utbot.framework.codegen.domain.builtin.mockitoClassId
19
20
import org.utbot.framework.codegen.domain.builtin.thenReturnMethodId
20
21
import org.utbot.framework.codegen.domain.builtin.whenMethodId
22
+ import org.utbot.framework.codegen.domain.builtin.whenStubberMethodId
21
23
import org.utbot.framework.codegen.domain.context.CgContext
22
24
import org.utbot.framework.codegen.domain.context.CgContextOwner
23
25
import org.utbot.framework.codegen.domain.models.CgAnonymousFunction
@@ -178,28 +180,37 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) {
178
180
val mockObject = newVar(model.classId, baseName = baseName, isMock = true ) { mock(modelClass) }
179
181
180
182
for ((executable, values) in model.mocks) {
181
- // void method
183
+ val matchers = mockitoArgumentMatchersFor(executable)
184
+
182
185
if (executable.returnType == voidClassId) {
183
- // void methods on mocks do nothing by default
184
- continue
185
- }
186
+ when (executable) {
187
+ // All constructors are considered like void methods, but it is proposed to be changed to test constructors better.
188
+ is ConstructorId -> continue
189
+ // Sometimes void methods are called explicitly, e.g. annotated with @Mock fields in Spring test classes.
190
+ // We would like to mark that this field is used and must not be removed from test class.
191
+ // Without `doNothing` call Intellij Idea suggests removing this field as unused.
192
+ is MethodId -> mockitoClassId[doNothingMethodId]()[whenStubberMethodId](mockObject)[executable](* matchers)
193
+ else -> error(" Only MethodId and ConstructorId was expected to appear in simple mocker but got $executable " )
194
+ }
195
+ } else {
196
+ when (executable) {
197
+ is MethodId -> {
198
+ if (executable.parameters.any { ! it.isAccessibleFrom(testClassPackageName) }) {
199
+ error(" Cannot mock method $executable with not accessible parameters" )
200
+ }
186
201
187
- when (executable) {
188
- is MethodId -> {
189
- if (executable.parameters.any { ! it.isAccessibleFrom(testClassPackageName) }) {
190
- error(" Cannot mock method $executable with not accessible parameters" )
191
- }
202
+ if (! executable.isAccessibleFrom(testClassPackageName)) {
203
+ error(" Cannot mock method $executable as it is not accessible from package $testClassPackageName " )
204
+ }
192
205
193
- val matchers = mockitoArgumentMatchersFor(executable)
194
- if (! executable.isAccessibleFrom(testClassPackageName)) {
195
- error(" Cannot mock method $executable as it is not accessible from package $testClassPackageName " )
206
+ val results = values.map { variableConstructor.getOrCreateVariable(it) }.toTypedArray()
207
+ `when `(mockObject[executable](* matchers)).thenReturn(executable.returnType, * results)
196
208
}
197
-
198
- val results = values.map { variableConstructor.getOrCreateVariable(it) }.toTypedArray()
199
- `when `(mockObject[executable](* matchers)).thenReturn(executable.returnType, * results)
209
+ else -> error(" Only MethodId was expected to appear in simple mocker but got $executable " )
200
210
}
201
- else -> error(" ConstructorId was not expected to appear in simple mocker but got $executable " )
202
211
}
212
+
213
+
203
214
}
204
215
205
216
return mockObject
0 commit comments