Description
Description
When we generate tests for a class with a field that hides field from superclass, incorrect tests are generated.
To Reproduce
Launch the plugin on class C
in the following example:
class A {
public int f;
void setF(int val) { this.f = val; }
int getF() { return this.f; }
}
class B extends A {
public int f;
}
public class C {
boolean foo(B b) {
return b.getF() == b.f + 1;
}
boolean bar(B b) { // replace body with return b.getF() == b.f and test generation will work ok
if (b.getF() == b.f)
return true;
else
return false;
}
}
Expected behavior
Correct tests with complete coverage are generated.
Actual behavior
For function bar
plugin generates incorrect test testBar_BGetFEqualsBF
(assertEquals in this test fails when launching).
For function foo
there is no testcase in which return value would be true.
Both problems disappear if we rename field in class A
to any other name.
Visual proofs (screenshots, logs, images)
Incorrect test generated:
@Test
@DisplayName("bar: BGetF -> return true")
public void testBar_BGetFEqualsBF() {
C c = new C();
B b = new B();
b.f = 1;
boolean actual = c.bar(b);
assertFalse(actual);
int finalBF = b.f;
assertEquals(0, finalBF);
}
Environment
plugin, Java11
Additional context
The problem also happens if we mark the field in class B
as private: in some of the generated tests there will be accesses to this private member, so generated tests are not even compilable. Try launching test generation on C.baz()
in this example:
package inheritanceIssues;
class A {
public int f;
void setF(int val) { this.f = val; }
int getF() { return this.f; }
}
class B extends A {
private int f;
}
public class C {
boolean baz(B b) {
return b.getF() == 1;
}
}
Metadata
Metadata
Assignees
Labels
Type
Projects
Status