Skip to content

StackOverflow when constructing an object with Assemble Model #812

Closed
@sofurihafe

Description

@sofurihafe

Description

When trying to generate tests for a method that returns an object with cross-reference relationship, StackOverflow exception is thrown.

To Reproduce

  1. Generate a test for the following code:
class FirstClass {
    SecondClass secondClass;

    FirstClass(SecondClass second) {
        this.secondClass = second;
    }
}

class SecondClass {
    FirstClass firstClass;

    SecondClass(FirstClass first) {
        this.firstClass = first;
    }
}

public class ClassWithCrossReferenceRelationship {
    public FirstClass returnFirstClass(int value) {
        if (value == 0) {
            return new FirstClass(new SecondClass(null));
        } else {
            FirstClass first = new FirstClass(null);
            first.secondClass = new SecondClass(first);

            return first;
        }
    }
}

Actual behavior

java.lang.StackOverflowError
	at java.base/java.util.ArrayList.addAll(ArrayList.java:702)
	at kotlin.collections.CollectionsKt___CollectionsKt.plus(_Collections.kt:3270)
	at org.utbot.framework.plugin.api.UtAssembleModel.getAllStatementsChain(Api.kt:504)
	at org.utbot.engine.ValueConstructor.constructFromAssembleModel(ValueConstructor.kt:327)
	at org.utbot.engine.ValueConstructor.construct(ValueConstructor.kt:190)
	at org.utbot.engine.ValueConstructor.value(ValueConstructor.kt:405)
	at org.utbot.engine.ValueConstructor.updateWithExecutableCallModel(ValueConstructor.kt:347)
	at org.utbot.engine.ValueConstructor.constructFromAssembleModel(ValueConstructor.kt:329)
	at org.utbot.engine.ValueConstructor.construct(ValueConstructor.kt:190)
	at org.utbot.engine.ValueConstructor.value(ValueConstructor.kt:405)
	at org.utbot.engine.ValueConstructor.updateWithExecutableCallModel(ValueConstructor.kt:347)
	at org.utbot.engine.ValueConstructor.constructFromAssembleModel(ValueConstructor.kt:329)
	at org.utbot.engine.ValueConstructor.construct(ValueConstructor.kt:190)
	at org.utbot.engine.ValueConstructor.value(ValueConstructor.kt:405)
	...
	...

Additional context

Setting useAssembleModelGenerator to false bypasses the problem.
However, it leads to NoSuchFieldException.

.
+-- JUnit Jupiter [OK]
| '-- ClassWithCrossReferenceRelationshipGeneratedTest [OK]
|   +-- returnFirstClass: value == 0 : True -> return new FirstClass(new SecondClass(nul... [X] modifiers
|   '-- returnFirstClass: value == 0 : False -> return first [OK]
+-- JUnit Vintage [OK]
'-- JUnit Platform Suite [OK]

Failures (1):
  JUnit Jupiter:ClassWithCrossReferenceRelationshipGeneratedTest:returnFirstClass: value == 0 : True -> return new FirstClass(new SecondClass(null))
    MethodSource [className = 'org.utbot.examples.codegen.deepequals.ClassWithCrossReferenceRelationshipGeneratedTest', methodName = 'testReturnFirstClass_ValueEqualsZero', methodParameterTypes = '']
    => java.lang.NoSuchFieldException: modifiers
       java.base/java.lang.Class.getDeclaredField(Class.java:2610)
       org.utbot.examples.codegen.deepequals.ClassWithCrossReferenceRelationshipGeneratedTest.setField(ClassWithCrossReferenceRelationshipGeneratedTest.java:312)
       org.utbot.examples.codegen.deepequals.ClassWithCrossReferenceRelationshipGeneratedTest.testReturnFirstClass_ValueEqualsZero(ClassWithCrossReferenceRelationshipGeneratedTest.java:67)
       java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
       java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       java.base/java.lang.reflect.Method.invoke(Method.java:568)
       org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
       org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
       org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
       [...]

Metadata

Metadata

Labels

ctg-bugIssue is a bug

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions