[英]Branch coverage in a java super class which is an abstract class
How do I solve a cobertura branch coverage issue which is being reflected in a concrete method of an abstract class. 我如何解决在抽象类的具体方法中反映的cobertura分支覆盖问题。
In the below snippet the equals method of the abstract class Currency contains a part where there is a check for the variables SID and Ab which comes after the condition if (getClass() != obj.getClass()). 在以下代码段中,抽象类Currency的equals方法包含一个部分,其中检查条件if(getClass()!= obj.getClass())之后的变量SID和Ab。 This part never gets covered
这部分永远不会被覆盖
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
if (getClass() != obj.getClass())
return false;
final Currency other = (Currency) obj;
if (this.getAb() == null) {
if (other.Ab() != null)
return false;
} else if (!this.getAb().equals(other.getAb()))
return false;
if (this.getSID() < 1 || (this.getSID() != other.getSID()))
return false;
return true;
}
I tried to cover these variables in the test class using the below method but it still doesn't get covered: 我尝试使用下面的方法在测试类中覆盖这些变量,但仍然无法实现:
Test class: 测试类别:
Currency currency = new Currency() {
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
};
Currency currency1 = new Currency() {
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
};
currency.setAb("SE3421");
currency1.setAb("SE3421");
assertFalse(currency.equals(currency1));
assertTrue((currency1.getAb()).equals(currency.getAb()));
Any help is appreciated. 任何帮助表示赞赏。
I would add a new test method, such as this. 我将添加一个新的测试方法,例如这样。
@test
public void test_equals() {
Currency currency = new Currency() { };
assertFalse(currency.equals(new String()));
}
One other comment: you should really implement the hashCode method as well. 另一则评论:您还应该真正实现hashCode方法。
In hashCode() it says: If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. 在hashCode()中表示:如果根据equals(Object)方法两个对象相等,则在两个对象中的每个对象上调用hashCode方法必须产生相同的整数结果。 If you only override equals() and not hashCode() your class violates this contract
如果您仅覆盖equals()而不是hashCode(),则您的类违反了此合同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.