@@ -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
@@ -190,28 +192,39 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) {
190
192
191
193
fun mockForVariable (model : UtCompositeModel , mockObject : CgVariable ) {
192
194
for ((executable, values) in model.mocks) {
193
- // void method
194
- if (executable.returnType == voidClassId) {
195
- // void methods on mocks do nothing by default
196
- continue
197
- }
195
+ val matchers = mockitoArgumentMatchersFor(executable)
198
196
199
- when (executable) {
200
- is MethodId -> {
201
- if (executable.parameters.any { ! it.isAccessibleFrom(testClassPackageName) }) {
202
- error(" Cannot mock method $executable with not accessible parameters" )
197
+ if (executable.returnType == voidClassId) {
198
+ when (executable) {
199
+ // All constructors are considered like void methods, but it is proposed to be changed to test constructors better.
200
+ is ConstructorId -> continue
201
+ // Sometimes void methods are called explicitly, e.g. annotated with @Mock fields in Spring test classes.
202
+ // We would like to mark that this field is used and must not be removed from test class.
203
+ // Without `doNothing` call Intellij Idea suggests removing this field as unused.
204
+ is MethodId -> {
205
+ + mockitoClassId[doNothingMethodId]()[whenStubberMethodId](mockObject)[executable](* matchers)
203
206
}
207
+ else -> error(" Only MethodId and ConstructorId was expected to appear in simple mocker but got $executable " )
208
+ }
209
+ } else {
210
+ when (executable) {
211
+ is MethodId -> {
212
+ if (executable.parameters.any { ! it.isAccessibleFrom(testClassPackageName) }) {
213
+ error(" Cannot mock method $executable with not accessible parameters" )
214
+ }
204
215
205
- val matchers = mockitoArgumentMatchersFor(executable)
206
- if (! executable.isAccessibleFrom(testClassPackageName)) {
207
- error(" Cannot mock method $executable as it is not accessible from package $testClassPackageName " )
208
- }
216
+ if (! executable.isAccessibleFrom(testClassPackageName)) {
217
+ error(" Cannot mock method $executable as it is not accessible from package $testClassPackageName " )
218
+ }
209
219
210
- val results = values.map { variableConstructor.getOrCreateVariable(it) }.toTypedArray()
211
- `when `(mockObject[executable](* matchers)).thenReturn(executable.returnType, * results)
220
+ val results = values.map { variableConstructor.getOrCreateVariable(it) }.toTypedArray()
221
+ `when `(mockObject[executable](* matchers)).thenReturn(executable.returnType, * results)
222
+ }
223
+ else -> error(" Only MethodId was expected to appear in simple mocker but got $executable " )
212
224
}
213
- else -> error(" ConstructorId was not expected to appear in simple mocker but got $executable " )
214
225
}
226
+
227
+
215
228
}
216
229
}
217
230
0 commit comments