UnitTestBot / UTBotJava

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

Test generator produces uncompiled code with the weird mock with Scanner for parameter with type Iterator<? extends Number> in test method #281

Open amandelpie opened 2 years ago

amandelpie commented 2 years ago

Description

The following snippet generates a bunch of tests including weird mock with Scanner (which implements Iterator interface) for the parameter for MUT method with type Iterator<? extends Number>

public class IterableLoverOfScanners {
    public static Double of(Iterator<? extends Number> values) {
        double sum = 0.0;

        while(values.hasNext()) {
            double value = values.next().doubleValue();
            sum+= value;
        }
        return sum;
    }
}

To Reproduce

Steps to reproduce the behavior:

  1. Run the test generation for the provided method and class during 120 seconds in the regime without mocks with JUnit5
  2. Find in the generated test one or few uncompiled tests
    /**
     * <pre>
     * Test
     * throws IllegalStateException in: while(values.hasNext())
     * </pre>
     */
    @Test
    @DisplayName("of: while(values.hasNext()) -> ThrowIllegalStateException")
    public void testOf_ThrowIllegalStateException() throws Exception {
        Scanner scanner = ((Scanner) createInstance("java.util.Scanner"));
        setField(scanner, "closed", true);

        /* This test fails because executable under testing com.google.common.custom.IterableLoverOfScanners.of
        produces Runtime exception java.lang.IllegalStateException: Scanner closed */
        IterableLoverOfScanners.of(scanner);
    }

Expected behavior

Found tests are compiled and Scanner is not used for mocking

Actual behavior

Tests are not compiled and Scanner is used for mocking

CaelmBleidd commented 2 years ago

The problem with types here is that the engine chose the right raw type (Scanner extends Iterator), but didn't take into account the generic parameter -- Scanner extends Iterator<String> doesn't suit with ??? extends Iterator<? extends Number>

It requires an additional fix in the engine, but I don't think it'll be fixed in the nearest future

amandelpie commented 2 years ago

Ok, @CaelmBleidd could we keep this ticket as open to remind us about possible problems? But probably it requires milestone "backlog"

CaelmBleidd commented 2 years ago

Sure, we should keep it open. Later, I'll create an additional task for the bug after further investigation

nikitavlaev commented 2 years ago

Also looks like ClassWithClassRefTest fails for Kotlin for the same reason. Check it plz

CaelmBleidd commented 2 years ago

Yes, looks like it. We expect to have Class<? extends List<?>> as a field, but got Class<java.lang.Object instead