2014-02-05 22 views
-1
  1. 我已經下載了ecllipse kepler - 來自ecllipse網站的最新版本。
  2. 我下載的JUnit從sourseforge網站4.10 jar文件(沒有得到在github複印件)
  3. 打開ecllipse我加入我的jar文件外罐(它已成功添加)
  4. 我說導入後的「進口static org.junit.Assert。*;「
  5. 然後是我在頂端@Test上寫的測試方法,然後是方法定義
  6. 現在ecllipse向我彈出一個@Test的錯誤 - 「無法從測試轉換爲註釋」,它甚至沒有給我作爲JUnit測試運行我的類的選項。

我是否錯過了某個地方的任何步驟?在ecllipse上安裝JUnit

我在這裏貼上我的代碼:提前

+0

製作當然你也有Java Build Path中的junit.jar - > Order and exports – AlexWien

+0

ok我已經在它裏面選擇了這個文件並且說OK。仍然錯誤不走。 – user1079065

回答

2

package test; 

import static org.junit.Assert.*; 

public class Test { 

    @Test 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     assertEquals(0, voidMethod(), 0); 
    } 

    static int voidMethod() 
    { 
     return 0; 
    } 
} 

感謝您需要導入JUnit測試類。使用這個:

import org.junit.Test;

爲了不與此導入衝突,最好還是將名稱命名爲Test以外的方法。

此外,您不能使用靜態方法作爲Junit測試方法,並且您的Junit測試不應該有參數。你會得到一個異常,說明測試方法不應該是靜態的,並且測試方法不應該帶參數。

Junit測試的重點在於您有許多可以獨立運行的獨立測試。您沒有任何參數傳入測試。您需要的一切應該在測試中或在使用@Before註釋之一進行測試之前進行設置。

嘗試是這樣的:

package test; 

import static org.junit.Assert.*; 
import org.junit.Test; 

public class TestClass { 

    @Test 
    public void myTestMethod() { 
     // TODO Auto-generated method stub 
     assertEquals(0, voidMethod(), 0); 
    } 

    static int voidMethod() 
    { 
     return 0; 
    } 
} 
0

不幸的是我已經把相同的名字我的課爲這實際上是裝箱問題「測試」 ..

非常感謝......