Skip to content

Commit e5a874f

Browse files
committed
Fix broken & unnecessary reflection
1 parent 9e6ee50 commit e5a874f

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/builtin/MockitoBuiltins.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ internal val doNothingMethodId = builtinStaticMethodId(
9797
internal val whenStubberMethodId = builtinMethodId(
9898
classId = stubberClassId,
9999
name = "when",
100-
returnType = objectClassId,
100+
returnType = stubberClassId,
101+
arguments = arrayOf(objectClassId)
101102
)
102103

103104
// TODO: for this method and other static methods implement some utils that allow calling

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/access/CgCallableAccessManager.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.utbot.framework.codegen.domain.builtin.getTargetException
1010
import org.utbot.framework.codegen.domain.builtin.invoke
1111
import org.utbot.framework.codegen.domain.builtin.newInstance
1212
import org.utbot.framework.codegen.domain.builtin.setAccessible
13+
import org.utbot.framework.codegen.domain.builtin.stubberClassId
1314
import org.utbot.framework.codegen.domain.context.CgContext
1415
import org.utbot.framework.codegen.domain.context.CgContextOwner
1516
import org.utbot.framework.codegen.domain.models.CgAllocateArray
@@ -192,8 +193,16 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
192193
when {
193194
// method of the current test class can be called on its 'this' instance
194195
currentTestClass == executable.classId && this isThisInstanceOf currentTestClass -> true
196+
195197
// method of a class can be called on an object of this class or any of its subtypes
196198
this.type isSubtypeOf executable.classId -> true
199+
200+
// We allow callers with [stubberClassId] classId to call any executable.
201+
// Example to see why: doNothing().when(entityManagerMock).persist(any()).
202+
// Here [persist()] cannot be called on [doNothing().when(entityManagerMock)],
203+
// because it requires [EntityManager] classId caller.
204+
this.type == stubberClassId -> true
205+
197206
else -> false
198207
}
199208

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/MockFrameworkManager.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) {
202202
// We would like to mark that this field is used and must not be removed from test class.
203203
// Without `doNothing` call Intellij Idea suggests removing this field as unused.
204204
is MethodId -> {
205+
// We allow [stubberClassId] to be a receiver of [executable].
206+
// See [CgExpression.canBeReceiverOf]
205207
+mockitoClassId[doNothingMethodId]()[whenStubberMethodId](mockObject)[executable](*matchers)
206208
}
207209
else -> error("Only MethodId and ConstructorId was expected to appear in simple mocker but got $executable")

0 commit comments

Comments
 (0)