Skip to content

Commit b5256bd

Browse files
committed
Add missed mock generator for static fields
1 parent b6c89b8 commit b5256bd

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,28 @@ import org.utbot.framework.plugin.api.MockInfo
66
import org.utbot.framework.plugin.api.MockStrategyApi.OTHER_PACKAGES
77
import kotlin.reflect.KClass
88
import org.junit.jupiter.api.Test
9+
import org.utbot.examples.mock.others.ClassWithStaticField
10+
import org.utbot.framework.plugin.api.CodegenLanguage
11+
import org.utbot.framework.plugin.api.UtCompositeModel
12+
import org.utbot.framework.plugin.api.UtNewInstanceInstrumentation
913
import org.utbot.testcheckers.eq
1014
import org.utbot.testcheckers.withoutConcrete
15+
import org.utbot.testing.CodeGeneration
1116
import org.utbot.testing.DoNotCalculate
17+
import org.utbot.testing.TestExecution
1218
import org.utbot.testing.UtValueTestCaseChecker
1319
import org.utbot.testing.singleMock
1420
import org.utbot.testing.singleMockOrNull
1521
import org.utbot.testing.value
1622

17-
internal class MockStaticFieldExampleTest : UtValueTestCaseChecker(testClass = MockStaticFieldExample::class) {
23+
internal class MockStaticFieldExampleTest : UtValueTestCaseChecker(
24+
testClass = MockStaticFieldExample::class,
25+
testCodeGeneration = true,
26+
pipelines = listOf(
27+
TestLastStage(CodegenLanguage.JAVA, lastStage = TestExecution),
28+
TestLastStage(CodegenLanguage.KOTLIN, lastStage = CodeGeneration)
29+
)
30+
) {
1831

1932
@Test
2033
fun testMockStaticField() {
@@ -68,6 +81,42 @@ internal class MockStaticFieldExampleTest : UtValueTestCaseChecker(testClass = M
6881
}
6982
}
7083

84+
@Test
85+
fun testCheckMocksInLeftAndRightAssignPartFinalField() {
86+
checkMocks(
87+
MockStaticFieldExample::checkMocksInLeftAndRightAssignPartFinalField,
88+
eq(1),
89+
{ mocks, _ ->
90+
val mock = mocks.singleMock("staticFinalField", ClassWithStaticField::foo)
91+
92+
mock.mocksStaticField(MockStaticFieldExample::class)
93+
},
94+
coverage = DoNotCalculate,
95+
mockStrategy = OTHER_PACKAGES
96+
)
97+
}
98+
99+
@Test
100+
fun testCheckMocksInLeftAndRightAssignPart() {
101+
checkMocksAndInstrumentation(
102+
MockStaticFieldExample::checkMocksInLeftAndRightAssignPart,
103+
eq(2),
104+
{ _, statics, _ ->
105+
val instrumentation = statics.single() as UtNewInstanceInstrumentation
106+
val model = instrumentation.instances.last() as UtCompositeModel
107+
108+
model.fields.isEmpty() // NPE
109+
},
110+
{ mocks, _, _ ->
111+
val mock = mocks.singleMock("staticField", ClassWithStaticField::foo)
112+
113+
mock.mocksStaticField(MockStaticFieldExample::class)
114+
},
115+
coverage = DoNotCalculate,
116+
mockStrategy = OTHER_PACKAGES
117+
)
118+
}
119+
71120
private fun MockInfo.mocksStaticField(kClass: KClass<*>) = when (val mock = mock) {
72121
is FieldMockTarget -> mock.ownerClassName == kClass.qualifiedName && mock.owner == null
73122
else -> false

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,10 +1910,10 @@ class Traverser(
19101910
with(simplificator) { simplifySymbolicValue(it) }
19111911
}
19121912

1913-
private fun readStaticField(fieldRef: StaticFieldRef): SymbolicValue {
1913+
private fun TraversalContext.readStaticField(fieldRef: StaticFieldRef): SymbolicValue {
19141914
val field = fieldRef.field
19151915
val declaringClassType = field.declaringClass.type
1916-
val staticObject = findOrCreateStaticObject(declaringClassType)
1916+
val staticObject = resolveInstanceForField(fieldRef)
19171917

19181918
val generator = (field.type as? RefType)?.let { refType ->
19191919
UtMockInfoGenerator { mockAddr ->
@@ -3592,7 +3592,12 @@ class Traverser(
35923592
staticFieldsUpdates.removeAll { update -> update.fieldId == it.fieldId }
35933593
fieldValuesUpdates.keys.removeAll { key -> key.fieldId == it.fieldId }
35943594

3595-
val value = createConst(it.type, it.name)
3595+
val generator = UtMockInfoGenerator { mockAddr ->
3596+
val fieldId = FieldId(it.declaringClass.id, it.name)
3597+
UtFieldMockInfo(it.type.classId, mockAddr, fieldId, ownerAddr = null)
3598+
}
3599+
3600+
val value = createConst(it.type, it.name, generator)
35963601
val valueToStore = if (value is ReferenceValue) {
35973602
value.addr
35983603
} else {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package org.utbot.examples.mock;
22

3+
import org.utbot.examples.mock.others.ClassWithStaticField;
34
import org.utbot.examples.mock.others.Generator;
45

56
public class MockStaticFieldExample {
67
@SuppressWarnings("unused")
78
private static Generator privateGenerator;
89
public static Generator publicGenerator;
910

11+
public static final ClassWithStaticField staticFinalField = new ClassWithStaticField();
12+
public static ClassWithStaticField staticField = new ClassWithStaticField();
13+
1014
public int calculate(int threshold) {
1115
int a = privateGenerator.generateInt();
1216
int b = publicGenerator.generateInt();
@@ -15,4 +19,20 @@ public int calculate(int threshold) {
1519
}
1620
return a + b + 1;
1721
}
22+
23+
// This test is associated with https://github.com/UnitTestBot/UTBotJava/issues/1533
24+
public int checkMocksInLeftAndRightAssignPartFinalField() {
25+
staticFinalField.intField = 5;
26+
staticFinalField.anotherIntField = staticFinalField.foo();
27+
28+
return staticFinalField.anotherIntField;
29+
}
30+
31+
// This test is associated with https://github.com/UnitTestBot/UTBotJava/issues/1533
32+
public int checkMocksInLeftAndRightAssignPart() {
33+
staticField.intField = 5;
34+
staticField.anotherIntField = staticField.foo();
35+
36+
return staticField.anotherIntField;
37+
}
1838
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.utbot.examples.mock.others;
2+
3+
public class ClassWithStaticField {
4+
public int intField;
5+
public int anotherIntField;
6+
7+
public int foo() {
8+
return 5;
9+
}
10+
}

0 commit comments

Comments
 (0)