此测试运行但失败。不知道为什么?有与长度级潜艇1如何JUnit测试构造函数?
@Test
public void testShipConstructor() {
assertTrue(Submarine.length == 1);
}
下面是类代码:
public abstract class Ship {
private int size;
public static int length;
protected Ship(int size, String type, String shortForm) {
this.size = size;
this.setType(type);
this.shortForm = shortForm;
}
public static void setLength(int length) {
}
public int getLength() {
return length;
}
int getSize() {
return size;
}
}
public class Submarine extends Ship {
private final static int SIZE = 1;
/**
* * Constructor, sets inherited length variable.
*/
public Submarine() {
super(SIZE, "Submarine", "#");
}
}
什么是'潜水艇'?你能展示这堂课吗? –
测试对象的行为,而不是它是否构造或其值。 – yehe
public abstract class Ship { \t \t private int size; \t \t \t public static int length; \t \t 保护\t船(INT大小,字符串型,字符串简短版本){ \t \t \t this.size =大小; \t \t \t this.setType(type); \t \t \t this.shortForm = shortForm; \t \t} \t \t \t \t公共静态无效setLength(INT长度){ \t \t} \t \t 公共\t INT的getLength(){ \t \t \t返回长度; \t \t} \t \t \t INT \t的getSize(){ \t \t \t返回大小; \t \t} \t} \t --------- \t \t 公共类海底延伸船舶{ \t \t私人最终静态INT SIZE = 1; \t \t/** \t \t *构造函数,设置继承的长度变量。 \t \t */ \t \t公共潜艇(){ \t \t \t超级(SIZE “海底”, “#”); \t \t} \t} – user742730