Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asserts are missing for anonymous class #1034

Open
alisevych opened this issue Sep 29, 2022 · 2 comments
Open

Asserts are missing for anonymous class #1034

alisevych opened this issue Sep 29, 2022 · 2 comments
Labels
comp-codegen Issue is related to code generator ctg-bug Issue is a bug

Comments

@alisevych
Copy link
Member

Description

If a method returns an anonymous class, the generated test method contains actual and expected, but doesn't assert them to be equal.

To Reproduce

  1. Run a project in IntelliJ Idea 2022.1.4 with one of the latest plugin builds from main branch
  2. Add the following code:
public class A {

    public interface Driver {
        public int getNumber(int n);
    }

    public Driver getDriver() {
        return new Driver() {
            @Override
            public int getNumber(int n) {
                return 2 * n - 1 ;
            }
        };
    }
}
  1. Use plugin to generate tests for getDriver() method.
  2. Open the generated test

Expected behavior

Test is supposed to contain assert of expected and actual value.

Actual behavior

Test contains actual and expected fields, but there is no assertion.

Visual proofs (screenshots, logs, images)

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;

import static org.utbot.runtime.utils.UtUtils.createInstance;
import static org.utbot.runtime.utils.UtUtils.setField;

public class ATest {
    ///region Test suites for executable A.getDriver

    ///region FUZZER: SUCCESSFUL EXECUTIONS for method getDriver()

    @Test
    @DisplayName("getDriver: arg_0 = A()")
    public void testGetDriver() throws Exception {
        A a = new A();

        A.Driver actual = a.getDriver();

        A.Driver expected = ((A.Driver) createInstance("A$1"));
        setField(expected, "A$1", "this$0", a);
    }
    ///endregion

    ///endregion
}

Environment

Windows 10 Pro
JDK 8

Additional context

The same issue can be seen for generating tests for the following code:

public class A {

    public int a, b = 10;

    public List<Integer> integerStream (int c) {
        return Arrays.asList(a, b, c);
    }
}
@alisevych
Copy link
Member Author

Currently UTBot compares only the state of the Objects.
That's a possible enhancement to compare the behavior of Objects.

@alisevych alisevych added the comp-codegen Issue is related to code generator label Feb 28, 2023
@tyuldashev
Copy link
Collaborator

Also generation for examples.objects.AbstractAnonymousClass#getInstance from UTBot Java project could be used to demonstrate the problem. Here is one of generated test:

    @Test
    @DisplayName("getInstance: x % 2 == 0 : False -> return new AbstractAnonymousClass() { @Override int constValue() { return 42 } @Override int add(int x) { return x + 15 } @Override public int methodWithOverride(int x, int y) { return x + 37 } }")
    public void testGetInstance_XRemainderOf2NotEqualsZero() throws Exception {
        AbstractAnonymousClass actual = AbstractAnonymousClass.getInstance(-255);

        AbstractAnonymousClass expected = ((AbstractAnonymousClass) createInstance("examples.objects.AbstractAnonymousClass$1"));

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-codegen Issue is related to code generator ctg-bug Issue is a bug
Projects
Status: Todo
Development

No branches or pull requests

2 participants