此測試運行但失敗。不知道爲什麼?有與長度級潛艇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