Skip to content

Commit

Permalink
Fix static mocking (#1142)
Browse files Browse the repository at this point in the history
  • Loading branch information
SBOne-Kenobi authored Oct 12, 2022
1 parent 0cbd535 commit 5af1f48
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.utbot.framework.concrete

import org.utbot.framework.plugin.api.util.signature
import java.lang.reflect.Method
import java.util.IdentityHashMap
import org.utbot.instrumentation.instrumentation.mock.computeKeyForMethod

/**
* Some information, which is computed after classes instrumentation.
Expand Down Expand Up @@ -66,7 +66,7 @@ class InstrumentationContext {
}

fun updateMocks(obj: Any?, method: Method, values: List<*>) {
updateMocks(obj, method.signature, values)
updateMocks(obj, computeKeyForMethod(method), values)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.utbot.framework.concrete

import org.utbot.common.withAccessibility
import org.utbot.framework.plugin.api.util.signature
import org.utbot.instrumentation.instrumentation.mock.MockConfig
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import org.utbot.common.withAccessibility
import org.utbot.instrumentation.instrumentation.mock.MockConfig
import org.utbot.instrumentation.instrumentation.mock.computeKeyForMethod


/**
Expand All @@ -31,7 +31,8 @@ class MethodMockController(
error("$method is an instance method, but instance is null!")
}

val id = instrumentationContext.methodSignatureToId[method.signature]
val computedSignature = computeKeyForMethod(method)
val id = instrumentationContext.methodSignatureToId[computedSignature]

isMockField = clazz.declaredFields.firstOrNull { it.name == MockConfig.IS_MOCK_FIELD + id }
?: error("No field ${MockConfig.IS_MOCK_FIELD + id} in $clazz")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import org.objectweb.asm.commons.AdviceAdapter
import org.objectweb.asm.commons.Method.getMethod
import org.utbot.framework.plugin.api.util.signature

object MockConfig {
const val IS_MOCK_FIELD = "\$__is_mock_"
}

/**
* Computes key for method that is used for mocking.
*/
fun computeKeyForMethod(internalType: String, methodSignature: String) =
"$internalType@$methodSignature"

fun computeKeyForMethod(method: Method) =
computeKeyForMethod(Type.getInternalName(method.declaringClass), method.signature)

class MockClassVisitor(
classVisitor: ClassVisitor,
mockGetter: Method,
Expand Down Expand Up @@ -73,7 +83,7 @@ class MockClassVisitor(
val isStatic = access and Opcodes.ACC_STATIC != 0
val isVoidMethod = Type.getReturnType(descriptor) == Type.VOID_TYPE

val computedSignature = name + descriptor
val computedSignature = computeKeyForMethod(internalClassName, "$name$descriptor")
val id = signatureToId.size
signatureToId[computedSignature] = id

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.utbot.instrumentation.examples.mock

import org.utbot.common.withAccessibility
import org.utbot.framework.plugin.api.util.signature
import org.utbot.instrumentation.instrumentation.instrumenter.Instrumenter
import org.utbot.instrumentation.instrumentation.mock.MockClassVisitor
import org.utbot.instrumentation.instrumentation.mock.MockConfig
import java.lang.reflect.Method
import java.util.IdentityHashMap
import kotlin.reflect.jvm.javaMethod
import org.objectweb.asm.Type
import org.utbot.common.withAccessibility
import org.utbot.instrumentation.instrumentation.instrumenter.Instrumenter
import org.utbot.instrumentation.instrumentation.mock.MockClassVisitor
import org.utbot.instrumentation.instrumentation.mock.MockConfig
import org.utbot.instrumentation.instrumentation.mock.computeKeyForMethod

/**
* Helper for generating tests with methods mocks.
Expand Down Expand Up @@ -52,8 +52,8 @@ class MockHelper(
error("Can't mock function returning void!")
}

val sign = method.signature
val methodId = mockClassVisitor.signatureToId[sign]
val computedSignature = computeKeyForMethod(method)
val methodId = mockClassVisitor.signatureToId[computedSignature]

val isMockField = instrumentedClazz.getDeclaredField(MockConfig.IS_MOCK_FIELD + methodId)
MockGetter.updateMocks(instance, method, mockedValues)
Expand Down Expand Up @@ -129,7 +129,7 @@ class MockHelper(
}

fun updateMocks(obj: Any?, method: Method, values: List<*>) {
updateMocks(obj, method.signature, values)
updateMocks(obj, computeKeyForMethod(method), values)
}
}
}

0 comments on commit 5af1f48

Please sign in to comment.