UnitTestBot / UTBotJava

Automated unit test generation and precise code analysis for Java
Apache License 2.0
136 stars 42 forks source link

`Iterable<? extends ClassName>` support #1603

Open alisevych opened 1 year ago

alisevych commented 1 year ago

Description

Need to generate correct data for Iterable<? extends ClassName>. Currently the generated Collection contains not only objects of the required class, but also arrays with such objects. So iterating over this collection fails with ClassCastException. The issue is actual for common classes and Enums.

To Reproduce

  1. Run the lrs project in IntelliJ Idea
  2. Use plugin to generate tests - version from main branch after release 2022.12 was used
  3. Generate tests with UnitTestBot for enum ThreeState
    • or for the following code example:
public class A {

    public class B {
        public int getNumber(int n) {
            return n * 2 - 1;
        }
    }

    public B findTheLimit(Iterable<? extends B> drivers) {
        int i = 0;
        int result = 0;
        for (B b : drivers) {
            result += b.getNumber(i + 10);
            if (result > 40) {
                return b;
            }
        }
        throw new IllegalStateException("Not found anything valuable");
    }
}

Expected behavior

Input data for public static ThreeState mostPositive(@NotNull Iterable<? extends ThreeState> states) should represent a Collection of ThreeState objects.

Actual behavior

There are two tests generated - for merge() and mostPositive() - that put an array as an element of a Collection. These tests fail with ClassCastException.

Visual proofs (screenshots, logs, images)

    @Test
    public void testMerge5() {
        HashSet states = new HashSet();
        states.add(null);
        B[] threeStateArray = {};
        states.add(threeStateArray);
        states.add(threeStateArray);

        /* This test fails because method [com.intellij.util.B.findTheLimit] produces [java.lang.ClassCastException: class [Lcom.intellij.util.B; cannot be cast to class com.intellij.util.B([Lcom.intellij.util.B; and com.intellij.util.Bare in unnamed module of loader org.utbot.instrumentation.process.HandlerClassesLoader @294975c8)]
            com.intellij.util.B.findTheLimit(A.java:55) */
        B.findTheLimit(states);
    }

Environment

Windows 10 Pro IntelliJ IDEA 2022.2.4 (not the last)

Additional context

Also there is one test with correct input data but

alisevych commented 1 year ago

@Markoutte After out today's discussion: I've verified on today's build from main - the issue is reproducing.