2016-04-28 46 views
0

我正在學習如何在GWT中執行單元測試。所以我創建了一個擴展GWTTestCase類的類,並在其中寫入測試方法。我是否需要爲每個使用GWTTestCase創建的單元測試方法添加前綴'test'?

問題是,如果除了繼承的'getModuleName'方法之外,如果我不在這個類(公共或私有)中甚至一個函數前面加上'test'這個詞,我會得到'初始化錯誤'。

現在我做錯了什麼或者是否添加此前綴是一個規則?如果其實際上是一個規則,這是什麼原因?在 '初始化錯誤' 的

public class hellotest extends GWTTestCase{ 
@Override 
public String getModuleName() { 
    // TODO Auto-generated method stub 
    return "hellopackage.Hello1"; 
} 

public void test() 
{ 
    assertTrue(true); 
} 

public void test2() 
{ 
    assertTrue(true); 
} 

public void notTest3()  //Produces initialization error 
{ 
    assertTrue(true); 
} 
} 

堆棧跟蹤: -

// Compiled from Filter.java (version 1.5 : 49.0, super bit) 
public abstract class org.junit.runner.manipulation.Filter { 

    // Field descriptor #14 Lorg/junit/runner/manipulation/Filter; 
    public static final org.junit.runner.manipulation.Filter ALL; 

    // Method descriptor #16()V 
    // Stack: 1, Locals: 1 
    public Filter(); 
    0 aload_0 [this] 
    1 invokespecial java.lang.Object() [1] 
    4 return 
     Line numbers: 
     [pc: 0, line: 17] 
     Local variable table: 
     [pc: 0, pc: 5] local: this index: 0 type: org.junit.runner.manipulation.Filter 

    // Method descriptor #22 (Lorg/junit/runner/Description;)Lorg/junit/runner/manipulation/Filter; 
    // Stack: 3, Locals: 1 
    public static org.junit.runner.manipulation.Filter matchMethodDescription(org.junit.runner.Description desiredDescription); 
    0 new org.junit.runner.manipulation.Filter$2 [2] 
    3 dup 
    4 aload_0 [desiredDescription] 
    5 invokespecial org.junit.runner.manipulation.Filter$2(org.junit.runner.Description) [3] 
    8 areturn 
     Line numbers: 
     [pc: 0, line: 48] 
     Local variable table: 
     [pc: 0, pc: 9] local: desiredDescription index: 0 type: org.junit.runner.Description 

    // Method descriptor #26 (Lorg/junit/runner/Description;)Z 
    public abstract boolean shouldRun(org.junit.runner.Description arg0); 

    // Method descriptor #28()Ljava/lang/String; 
    public abstract java.lang.String describe(); 

    // Method descriptor #30 (Ljava/lang/Object;)V 
    // Stack: 2, Locals: 3 
    public void apply(java.lang.Object child) throws org.junit.runner.manipulation.NoTestsRemainException; 
    0 aload_1 [child] 
    1 instanceof org.junit.runner.manipulation.Filterable [4] 
    4 ifne 8 
    7 return 
    8 aload_1 [child] 
    9 checkcast org.junit.runner.manipulation.Filterable [4] 
    12 astore_2 [filterable] 
    13 aload_2 [filterable] 
    14 aload_0 [this] 
    15 invokeinterface org.junit.runner.manipulation.Filterable.filter(org.junit.runner.manipulation.Filter) : void [5] [nargs: 2] 
    20 return 
     Line numbers: 
     [pc: 0, line: 93] 
     [pc: 7, line: 94] 
     [pc: 8, line: 96] 
     [pc: 13, line: 97] 
     [pc: 20, line: 98] 
     Local variable table: 
     [pc: 0, pc: 21] local: this index: 0 type: org.junit.runner.manipulation.Filter 
     [pc: 0, pc: 21] local: child index: 1 type: java.lang.Object 
     [pc: 13, pc: 21] local: filterable index: 2 type: org.junit.runner.manipulation.Filterable 

    // Method descriptor #38 (Lorg/junit/runner/manipulation/Filter;)Lorg/junit/runner/manipulation/Filter; 
    // Stack: 5, Locals: 3 
    public org.junit.runner.manipulation.Filter intersect(org.junit.runner.manipulation.Filter second); 
    0 aload_1 [second] 
    1 aload_0 [this] 
    2 if_acmpeq 12 
    5 aload_1 [second] 
    6 getstatic org.junit.runner.manipulation.Filter.ALL : org.junit.runner.manipulation.Filter [6] 
    9 if_acmpne 14 
    12 aload_0 [this] 
    13 areturn 
    14 aload_0 [this] 
    15 astore_2 [first] 
    16 new org.junit.runner.manipulation.Filter$3 [7] 
    19 dup 
    20 aload_0 [this] 
    21 aload_2 [first] 
    22 aload_1 [second] 
    23 invokespecial org.junit.runner.manipulation.Filter$3(org.junit.runner.manipulation.Filter, org.junit.runner.manipulation.Filter, org.junit.runner.manipulation.Filter) [8] 
    26 areturn 
     Line numbers: 
     [pc: 0, line: 105] 
     [pc: 12, line: 106] 
     [pc: 14, line: 108] 
     [pc: 16, line: 109] 
     Local variable table: 
     [pc: 0, pc: 27] local: this index: 0 type: org.junit.runner.manipulation.Filter 
     [pc: 0, pc: 27] local: second index: 1 type: org.junit.runner.manipulation.Filter 
     [pc: 16, pc: 27] local: first index: 2 type: org.junit.runner.manipulation.Filter 

    // Method descriptor #16()V 
    // Stack: 2, Locals: 0 
    static {}; 
    0 new org.junit.runner.manipulation.Filter$1 [9] 
    3 dup 
    4 invokespecial org.junit.runner.manipulation.Filter$1() [10] 
    7 putstatic org.junit.runner.manipulation.Filter.ALL : org.junit.runner.manipulation.Filter [6] 
    10 return 
     Line numbers: 
     [pc: 0, line: 21] 

    Inner classes: 
    [inner class info: #2 org/junit/runner/manipulation/Filter$2, outer class info: #0 
    inner name: #0, accessflags: 8 static], 
    [inner class info: #7 org/junit/runner/manipulation/Filter$3, outer class info: #0 
    inner name: #0, accessflags: 0 default], 
    [inner class info: #9 org/junit/runner/manipulation/Filter$1, outer class info: #0 
    inner name: #0, accessflags: 8 static] 
} 

回答

2

GWTTestCase擴展了JUnit的TestCase 3,並在JUnit 3中,試驗方法確實應該開始與test

原因在於它是如何設計JUnit的:它在測試用例中搜索以test開頭的公共無效方法。這是在Java 5之前完成的,當時註釋尚不存在。現在,JUnit 4(和5)使用註釋來「標記」測試方法。

+0

如果我想在擴展gwtTestCase的類中使用「非測試用例」方法,該怎麼辦?這可能嗎? –

+0

當然可以。只是不要將它命名爲testXxx。 –

+0

如果我在該類中創建任何沒有前綴「測試」的函數,如果它的私有或不是,我會得到初始化錯誤。 –

1

除了已經回答的問題「技術上是否需要」你也可以考慮約定。

我見過的單元測試大多數都是從testWhatever()開始的。

關鍵是:命名是編寫乾淨,可讀,可維護代碼的核心元素。生產代碼就是這樣;但是嘿,很多人認爲你的測試代碼比生產代碼更重要......所以編寫乾淨,可讀,可維護的測試代碼是軟件開發的一個重要部分。

換句話說:大概大多數人都認爲測試方法是以「測試」開始的。但最後:關鍵是名稱會說明實際測試的內容;所以test(),test1()等...對於測試方法來說真的是非常糟糕的名字。 (並注意:如果你有興趣編寫好的測試,請查看assertThat;並忘記使用任何其他assert調用)。

+0

好點的結果。其實我在這裏用的名字只是爲了讓事情變得簡單。我實際上並不使用測試,test1等名稱。 –

相關問題