Skip to content

Commit a2407f4

Browse files
authored
Add missed mockInfoGenerator for system static objects (#2014)
1 parent aa52b28 commit a2407f4

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockStaticMethodExampleTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.utbot.framework.util.singleStaticMethod
77
import org.utbot.framework.util.singleValue
88
import org.junit.jupiter.api.Test
99
import org.utbot.framework.plugin.api.CodegenLanguage
10+
import org.utbot.framework.plugin.api.util.id
1011
import org.utbot.testcheckers.eq
1112
import org.utbot.testing.CodeGeneration
1213
import org.utbot.testing.Compilation
@@ -48,4 +49,14 @@ internal class MockStaticMethodExampleTest : UtValueTestCaseChecker(
4849
mockStrategy = MockStrategyApi.OTHER_PACKAGES
4950
)
5051
}
52+
53+
@Test
54+
fun testMockStaticMethodFromAlwaysMockClass() {
55+
checkMocksAndInstrumentation(
56+
MockStaticMethodExample::mockStaticMethodFromAlwaysMockClass,
57+
eq(1),
58+
coverage = DoNotCalculate,
59+
additionalMockAlwaysClasses = setOf(System::class.id)
60+
)
61+
}
5162
}

utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,8 @@ class Traverser(
634634
): Pair<SymbolicStateUpdate, SymbolicValue?> {
635635
val jClass = type.id.jClass
636636

637-
// symbolic value for enum class itself
638-
val enumClassValue = findOrCreateStaticObject(type)
637+
// symbolic value for enum class itself, we don't want to have mocks for this value
638+
val enumClassValue = findOrCreateStaticObject(type, mockInfoGenerator = null)
639639

640640
// values for enum constants
641641
val enumConstantConcreteValues = jClass.enumConstants.filterIsInstance<Enum<*>>()
@@ -708,8 +708,10 @@ class Traverser(
708708
val initializedFieldUpdate =
709709
MemoryUpdate(initializedStaticFields = persistentHashSetOf(fieldId))
710710

711+
val mockInfoGenerator = UtMockInfoGenerator { addr -> UtStaticObjectMockInfo(declaringClass.id, addr) }
712+
711713
val objectUpdate = objectUpdate(
712-
instance = findOrCreateStaticObject(declaringClass.type),
714+
instance = findOrCreateStaticObject(declaringClass.type, mockInfoGenerator),
713715
field = field,
714716
value = valueToExpression(symbolicValue, field.type)
715717
)
@@ -2187,7 +2189,7 @@ class Traverser(
21872189
*/
21882190
private fun findOrCreateStaticObject(
21892191
classType: RefType,
2190-
mockInfoGenerator: UtMockInfoGenerator? = null
2192+
mockInfoGenerator: UtMockInfoGenerator?
21912193
): ObjectValue {
21922194
val fromMemory = locateStaticObject(classType)
21932195

utbot-sample/src/main/java/org/utbot/examples/mock/MockStaticMethodExample.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ public int useStaticMethod() {
1111

1212
return 0;
1313
}
14+
15+
public void mockStaticMethodFromAlwaysMockClass() {
16+
System.out.println("example");
17+
}
1418
}

0 commit comments

Comments
 (0)