Java Junit toString部分覆盖
我在NetBeans中对字符串类进行junit测试:Java Junit toString部分覆盖,java,junit,tostring,Java,Junit,Tostring,我在NetBeans中对字符串类进行junit测试: public String toString() { try { return "I am ContainerTruck " + getIdentifier() + ".\n\tI am at " + getLocation() + " and am heading to " + getDestination() + ".\
public String toString() {
try {
return "I am ContainerTruck " + getIdentifier() + ".\n\tI am at "
+ getLocation() + " and am heading to " + getDestination()
+ ".\n\tMy load is " + getCurrentLoadWeight() + " and my max load is "
+ getMaxLoadWeight() + ".\n\tDistance to my destination is "
+ String.format("%4.2f", distance(getDestination())) + ". "
+ (atDestination() ? "I am there!" : "I'm not there yet");
} catch (InvalidDataException ex) {
return ex.getMessage();
}
}
netbeans一直说我的测试得到了部分覆盖:
public void testToString(){
double lX = 1.0;
double lY = 1.0;
double lZ = 1.0;
double dX = 1.0;
double dY = 1.0;
double dZ = 1.0;
double spd = 1.0;
double mxSpd = 1.0;
double mlw = 1.0;
try{
ContainerTruck result = new ContainerTruck(lX, lY, lZ, dX, dY, dZ, spd, mxSpd, mlw);
IdentifiableImpl imp = new IdentifiableImpl(result.getIdentifier());
assertNotNull(result);
assertEquals("I am ContainerTruck " + imp.getIdentifier() + ".\n\tI am at [1.00, 1.00, 1.00] and am heading to [1.00, 1.00, 1.00].\n\tMy load is 0.0 and my max load is 1.0.\n\tDistance to my destination is 0.00. I am there!", result.toString());
assertEquals(imp.getIdentifier(),result.getIdentifier());
assertEquals(0.0, result.getCurrentLoadWeight(), delta);
assertEquals(1.0, result.getLocationZ(), delta);
assertEquals(1.0, result.getLocationY(), delta);
assertEquals(1.0, result.getLocationX(), delta);
assertEquals(1.0, result.getMaxLoadWeight(), delta);
assertEquals(1.0, result.getDestinationX(), delta);
assertEquals(true, result.atDestination());
assertEquals(1.0, result.getDestinationY(), delta);
assertEquals(1.0, result.getDestinationZ(), delta);
assertEquals(1.0, result.getSpeed(), delta);
assertEquals(1.0, result.getMaxSpeed(), delta);
}catch (InvalidDataException ex) {
fail(ex.getMessage());
}
}
有人能给我解释一下为什么这仍然被认为是部分覆盖吗?您的toString方法考虑的只是一个愉快的场景,您的一些方法不会抛出异常,因此它永远不会进入catch块
您应该尝试调用该方法,以便进入catch块,在该块中可以返回异常的消息 啊,谢谢!,但我仍然认为我有部分保险问题。基于netbeans,部分覆盖在字符串行上,我不明白为什么会这样。