@@ -22,6 +22,7 @@ import org.utbot.framework.codegen.domain.context.CgContext
22
22
import org.utbot.framework.codegen.domain.context.CgContextOwner
23
23
import org.utbot.framework.codegen.domain.models.CgAnonymousFunction
24
24
import org.utbot.framework.codegen.domain.models.CgAssignment
25
+ import org.utbot.framework.codegen.domain.models.CgBreakStatement
25
26
import org.utbot.framework.codegen.domain.models.CgConstructorCall
26
27
import org.utbot.framework.codegen.domain.models.CgDeclaration
27
28
import org.utbot.framework.codegen.domain.models.CgExecutableCall
@@ -226,7 +227,6 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object
226
227
nameGenerator.variableName(MOCK_CLASS_COUNTER_NAME ),
227
228
CgConstructorCall (ConstructorId (atomicIntegerClassId, emptyList()), emptyList())
228
229
)
229
- + mockClassCounter
230
230
231
231
val mocksExecutablesAnswers = mock
232
232
.instances
@@ -242,13 +242,19 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object
242
242
mocksExecutablesAnswers,
243
243
mockClassCounter.variable
244
244
)
245
+
246
+ if (mockConstructionInitializer.isMockClassCounterRequired) {
247
+ // We should insert the counter declaration only if we use this counter, for better readability.
248
+ + mockClassCounter
249
+ }
250
+
245
251
val mockedConstructionDeclaration = CgDeclaration (
246
252
MockitoStaticMocking .mockedConstructionClassId,
247
253
nameGenerator.variableName(MOCKED_CONSTRUCTION_NAME ),
248
- mockConstructionInitializer
254
+ mockConstructionInitializer.mockConstructionCall
249
255
)
250
256
resources + = mockedConstructionDeclaration
251
- + CgAssignment (mockedConstructionDeclaration.variable, mockConstructionInitializer)
257
+ + CgAssignment (mockedConstructionDeclaration.variable, mockConstructionInitializer.mockConstructionCall )
252
258
mockedStaticConstructions + = classId
253
259
}
254
260
@@ -317,9 +323,9 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object
317
323
private fun mockConstruction (
318
324
clazz : CgExpression ,
319
325
classId : ClassId ,
320
- mocksWhenAnswers : List <MutableMap <ExecutableId , List <UtModel >>>,
326
+ mocksWhenAnswers : List <Map <ExecutableId , List <UtModel >>>,
321
327
mockClassCounter : CgVariable
322
- ): CgMethodCall {
328
+ ): MockConstructionBlock {
323
329
val mockParameter = variableConstructor.declareParameter(
324
330
classId,
325
331
nameGenerator.variableName(classId.simpleName, isMock = true )
@@ -333,6 +339,8 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object
333
339
for ((index, mockWhenAnswers) in mocksWhenAnswers.withIndex()) {
334
340
val statements = mutableListOf<CgStatement >()
335
341
for ((executable, values) in mockWhenAnswers) {
342
+ // For now, all constructors are considered like void methods, but it is proposed to be changed
343
+ // for better constructors testing.
336
344
if (executable.returnType == voidClassId) continue
337
345
338
346
when (executable) {
@@ -343,7 +351,7 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object
343
351
mocker.`when `(mockParameter[executable](* matchers))[thenReturnMethodId](* results)
344
352
)
345
353
}
346
- else -> error(" Expected MethodId but got ConstructorId $executable " )
354
+ is ConstructorId -> error(" Expected MethodId but got ConstructorId $executable " )
347
355
}
348
356
}
349
357
@@ -352,15 +360,36 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object
352
360
353
361
val switchCase = CgSwitchCase (mockClassCounter[atomicIntegerGet](), caseLabels)
354
362
363
+ // If all switch-case labels are empty,
364
+ // it means we do not need this switch and mock counter itself at all.
365
+ val mockConstructionBody = if (caseLabels.map { it.statements }.all { it.isEmpty() }) {
366
+ emptyList()
367
+ } else {
368
+ listOf (switchCase, CgStatementExecutableCall (mockClassCounter[atomicIntegerGetAndIncrement]()))
369
+ }
370
+
355
371
val answersBlock = CgAnonymousFunction (
356
372
voidClassId,
357
373
listOf (mockParameter, contextParameter).map { CgParameterDeclaration (it, isVararg = false ) },
358
- listOf (switchCase, CgStatementExecutableCall (mockClassCounter[atomicIntegerGetAndIncrement]()))
374
+ mockConstructionBody
359
375
)
360
376
361
- return mockitoClassId[MockitoStaticMocking .mockConstructionMethodId](clazz, answersBlock)
377
+ return MockConstructionBlock (
378
+ mockitoClassId[MockitoStaticMocking .mockConstructionMethodId](clazz, answersBlock),
379
+ mockConstructionBody.isNotEmpty()
380
+ )
362
381
}
363
382
383
+ /* *
384
+ * Represents a body for invocation of the [MockitoStaticMocking.mockConstructionMethodId] method and information
385
+ * whether we need to use a counter for different mocking invocations
386
+ * (i.e., on each mocking we expect possibly different results).
387
+ */
388
+ private data class MockConstructionBlock (
389
+ val mockConstructionCall : CgMethodCall ,
390
+ val isMockClassCounterRequired : Boolean
391
+ )
392
+
364
393
private fun mockStatic (clazz : CgExpression ): CgMethodCall =
365
394
mockitoClassId[MockitoStaticMocking .mockStaticMethodId](clazz)
366
395
0 commit comments