Skip to content

Bad test is produced when use concrete classes for implementing interface with generic #2450

Closed
@Markoutte

Description

@Markoutte

Description

When there're only one implementation of some interface with generic type fuzzing choose this implementation even it is not suitable for a method.

To Reproduce

  1. Use only fuzzer
  2. Generate test for method test.
public class Implementations {

    public interface A<T> {
        T getValue();
    }

    public static class AImpl implements A<String> {

        private final String value;

        public AImpl(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }

    public static int test(A<Integer> value) {
        if (value.getValue() < 0) {
            return 0;
        }
        return value.getValue();
    }
}

Expected behavior

The only one method with null is generated, because there're no other options to create this class by fuzzer.

Actual behavior

2 tests are generated: with null and other with AImpl:

    @Test
    @DisplayName("test: arg_0 = AImpl(String) -> throw ClassCastException")
    public void testTestThrowsCCE() {
        AImpl aImpl = new AImpl("-3E");
        
        /* This test fails because method [sanity.Implementations.test] produces [java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer]
            sanity.Implementations.test(Implementations.java:24) */
        Implementations.test(aImpl);
    }

This test cannot be compiled.

Additional context

The issue was introduced by 19fc16c

Metadata

Metadata

Assignees

Labels

comp-fuzzingIssue is related to the fuzzingctg-bugIssue is a bug

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions