Skip to content

Generated parametrized test tries to access non-present field #510

Closed
@sofurihafe

Description

@sofurihafe

To Reproduce

  1. Enable parametrized test generation for samples,
  2. Run InstanceOfExampleTest.

Actual behavior

Generated parametrized test looks like this:

public class InstanceOfExampleGeneratedTest {
    ///region Test suites for executable org.utbot.examples.casts.InstanceOfExample.symbolicInstanceOf
    
    ///region Parameterized test for method symbolicInstanceOf(org.utbot.examples.casts.CastClass[], int)
    
    @ParameterizedTest
    @MethodSource("provideDataForSymbolicInstanceOf")
    public void parameterizedTestsForSymbolicInstanceOf(InstanceOfExample instanceOfExample,
    org.utbot.examples.casts.CastClass[] castClassArray,
    int int1,
    CastClass expectedResult,
    Class<Throwable> expectedError
    ) throws IllegalAccessException, NoSuchFieldException  {
        try {
            CastClass actual = instanceOfExample.symbolicInstanceOf(castClassArray, int1);
            
            if (expectedResult == null) {
                assertNull(actual);
            } else {
                
                int expectedResultZ = ((Integer) getFieldValue(expectedResult, "z"));
                int actualZ = ((Integer) getFieldValue(actual, "z"));
                assertEquals(expectedResultZ, actualZ);
                
                int expectedResultX = expectedResult.x;
                int actualX = actual.x;
                assertEquals(expectedResultX, actualX);
                
                int expectedResultDefaultValue = expectedResult.defaultValue;
                int actualDefaultValue = actual.defaultValue;
                assertEquals(expectedResultDefaultValue, actualDefaultValue);
            }
        } catch (java.lang.Throwable throwable) {
            assertTrue(expectedError.isInstance(throwable));
        }
    }
    ///endregion
    
    ///endregion
    
    ///region Data providers and utils methods
    
    public static java.util.ArrayList<org.junit.jupiter.params.provider.Arguments> provideDataForSymbolicInstanceOf() {
        java.util.ArrayList<org.junit.jupiter.params.provider.Arguments> argList = new java.util.ArrayList<org.junit.jupiter.params.provider.Arguments>();
        
        <...>

        {
            InstanceOfExample instanceOfExample = new InstanceOfExample();
            org.utbot.examples.casts.CastClass[] castClassArray = {null, null};
            CastClassSecondSucc castClassSecondSucc = new CastClassSecondSucc();
            castClassSecondSucc.d = 20;
            castClassSecondSucc.defaultValue = 5;
            
            argList.add(org.junit.jupiter.params.provider.Arguments.arguments(instanceOfExample, castClassArray, 1, castClassSecondSucc, null));
        }

        <...>
        
        return argList;
    }
    
    private static Object getFieldValue(Object obj, String fieldName) throws IllegalAccessException, NoSuchFieldException {
        <...>
    }
    ///endregion
}

The problem is that a field z is a field of CastClassFirstSucc class, but provided class is CastClassSecondSucc, which has d field. So getFieldValue fails.

Expected behavior

Currently there are two ideas:

  1. Create a separate parametrized test for each type of expected result in executions
  2. If there is a class that is a subtype of all result classes (e.g. CastClassSecondSucc is an inheritor of not only CastClass but a CastClassFirstSucc also), we can use it in arbitraryExecution.

Metadata

Metadata

Labels

comp-codegenIssue is related to code generatorctg-bugIssue is a bug

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions