Skip to content

UTbot generates test which tries to access outer class and throws NPE #917

@tyuldashev

Description

@tyuldashev

Description

Utbot intentionally sets outer class reference to null to generate NPE exception.

To Reproduce

  1. In settings of UTBot set 'Code analysis' slider to 'Deeper'
  2. Generate tests for Outer.Inner.test method in following code:
public class Outer {
    public void publicMethod() {
        System.out.println("Outer.publicMethod");
    }

    class Inner {
        void test() {
            publicMethod();
        }
    }
}

Expected behavior
One test which invokes Outer.Inner.test() is generated.

Actual behavior
Two tests are generated and second one fails with NPE. Note that test intentionally sets null as reference to outer class, which causes NullPointerException. Such test seems not helpful for users and should not be generated.

    @Test
    @DisplayName("test: privateMethod() : True -> ThrowNullPointerException")
    public void testTest_OuterPrivateMethod_1() throws Exception {
        Inner inner = ((Inner) createInstance("org.test.Outer$Inner"));
        setField(inner, "org.test.Outer$Inner", "this$0", null);
        
        /* This test fails because method [org.test.Outer$Inner.test] produces [java.lang.NullPointerException]
            org.test.Outer$Inner.test(Outer.java:10) */
        inner.test();
    }

Metadata

Metadata

Assignees

Labels

ctg-bugIssue is a bug

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions