Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@ import org.utbot.framework.plugin.api.MockInfo
import org.utbot.framework.plugin.api.MockStrategyApi.OTHER_PACKAGES
import kotlin.reflect.KClass
import org.junit.jupiter.api.Test
import org.utbot.examples.mock.others.ClassWithStaticField
import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.framework.plugin.api.UtCompositeModel
import org.utbot.framework.plugin.api.UtNewInstanceInstrumentation
import org.utbot.testcheckers.eq
import org.utbot.testcheckers.withoutConcrete
import org.utbot.testing.CodeGeneration
import org.utbot.testing.DoNotCalculate
import org.utbot.testing.TestExecution
import org.utbot.testing.UtValueTestCaseChecker
import org.utbot.testing.singleMock
import org.utbot.testing.singleMockOrNull
import org.utbot.testing.value

internal class MockStaticFieldExampleTest : UtValueTestCaseChecker(testClass = MockStaticFieldExample::class) {
internal class MockStaticFieldExampleTest : UtValueTestCaseChecker(
testClass = MockStaticFieldExample::class,
testCodeGeneration = true,
pipelines = listOf(
TestLastStage(CodegenLanguage.JAVA, lastStage = TestExecution),
// disabled due to https://github.com/UnitTestBot/UTBotJava/issues/88
TestLastStage(CodegenLanguage.KOTLIN, lastStage = CodeGeneration)
)
) {

@Test
fun testMockStaticField() {
Expand Down Expand Up @@ -68,6 +82,42 @@ internal class MockStaticFieldExampleTest : UtValueTestCaseChecker(testClass = M
}
}

@Test
fun testCheckMocksInLeftAndRightAssignPartFinalField() {
checkMocks(
MockStaticFieldExample::checkMocksInLeftAndRightAssignPartFinalField,
eq(1),
{ mocks, _ ->
val mock = mocks.singleMock("staticFinalField", ClassWithStaticField::foo)

mock.mocksStaticField(MockStaticFieldExample::class)
},
coverage = DoNotCalculate,
mockStrategy = OTHER_PACKAGES
)
}

@Test
fun testCheckMocksInLeftAndRightAssignPart() {
checkMocksAndInstrumentation(
MockStaticFieldExample::checkMocksInLeftAndRightAssignPart,
eq(2),
{ _, statics, _ ->
val instrumentation = statics.single() as UtNewInstanceInstrumentation
val model = instrumentation.instances.last() as UtCompositeModel

model.fields.isEmpty() // NPE
},
{ mocks, _, _ ->
val mock = mocks.singleMock("staticField", ClassWithStaticField::foo)

mock.mocksStaticField(MockStaticFieldExample::class)
},
coverage = DoNotCalculate,
mockStrategy = OTHER_PACKAGES
)
}

private fun MockInfo.mocksStaticField(kClass: KClass<*>) = when (val mock = mock) {
is FieldMockTarget -> mock.ownerClassName == kClass.qualifiedName && mock.owner == null
else -> false
Expand Down
11 changes: 8 additions & 3 deletions utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1910,10 +1910,10 @@ class Traverser(
with(simplificator) { simplifySymbolicValue(it) }
}

private fun readStaticField(fieldRef: StaticFieldRef): SymbolicValue {
private fun TraversalContext.readStaticField(fieldRef: StaticFieldRef): SymbolicValue {
val field = fieldRef.field
val declaringClassType = field.declaringClass.type
val staticObject = findOrCreateStaticObject(declaringClassType)
val staticObject = resolveInstanceForField(fieldRef)

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

val value = createConst(it.type, it.name)
val generator = UtMockInfoGenerator { mockAddr ->
val fieldId = FieldId(it.declaringClass.id, it.name)
UtFieldMockInfo(it.type.classId, mockAddr, fieldId, ownerAddr = null)
}

val value = createConst(it.type, it.name, generator)
val valueToStore = if (value is ReferenceValue) {
value.addr
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.utbot.examples.mock;

import org.utbot.examples.mock.others.ClassWithStaticField;
import org.utbot.examples.mock.others.Generator;

public class MockStaticFieldExample {
@SuppressWarnings("unused")
private static Generator privateGenerator;
public static Generator publicGenerator;

public static final ClassWithStaticField staticFinalField = new ClassWithStaticField();
public static ClassWithStaticField staticField = new ClassWithStaticField();

public int calculate(int threshold) {
int a = privateGenerator.generateInt();
int b = publicGenerator.generateInt();
Expand All @@ -15,4 +19,20 @@ public int calculate(int threshold) {
}
return a + b + 1;
}

// This test is associated with https://github.com/UnitTestBot/UTBotJava/issues/1533
public int checkMocksInLeftAndRightAssignPartFinalField() {
staticFinalField.intField = 5;
staticFinalField.anotherIntField = staticFinalField.foo();

return staticFinalField.anotherIntField;
}

// This test is associated with https://github.com/UnitTestBot/UTBotJava/issues/1533
public int checkMocksInLeftAndRightAssignPart() {
staticField.intField = 5;
staticField.anotherIntField = staticField.foo();

return staticField.anotherIntField;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.utbot.examples.mock.others;

public class ClassWithStaticField {
public int intField;
public int anotherIntField;

public int foo() {
return 5;
}
}