2016-12-03 266 views
-4

我必須使用JUnit Test來檢查我的類,並且無法讓它們工作。很簡單的測試,我想看起來像這樣:在JUnit測試中找不到符號

@Test 
    public void points_shouldCreatInstance() { 
     assertEquals(1.0f,2.0f, Point.Point(1.0f,2.0f)); 
    } 

,我想測試這個類:

public class Point { 

    float x; 
    float y; 

    public Point(float x, float y){ 
     this.x=x; 
     this.y=y; 
    } 

    public float get_x(){ 
     return this.x; 
    } 

    public float get_y(){ 
     return this.y; 
    } 
... 
} 

但Netbeans的告訴我,它無法找到符號(第二)點在Point.Point(1.0f,2.0f)); 我相信這很明顯,但我無法找到任何有關JUnit的書面文檔。

+0

'公共點(浮動的x,浮子Y)'是[構造](HTTPS://文檔。 oracle.com/javase/tutorial/java/javaOO/constructors.html),這就是'new Point(1.0f,1.0f)'調用的東西。 'Point.Point'將引用該類上的靜態方法,這是錯誤告訴你的,你沒有。這與JUnit **無關,你顯然不熟悉Java OOP。 – jonrsharpe

+0

比*「不起作用」*更具體。如果你的意思是測試不通過;當然它不,這不是你如何使用'assertEquals'。你的測試實際上沒有任何意義。 – jonrsharpe

回答

3

Point(1.0f,2.0f)是一個構造函數調用,不static方法調用(請不要使用點運算符),所以你不能把喜歡Point.Point(1.0f,2.0f)),這是不正確。
這裏,爲了測試Point類,您需要使用new運算符(如new Point(1.0f,2.0f))創建Point類對象。

測試Point類正確的方法示於下面的代碼與註釋:

 @Test 
    public void points_shouldCreatInstance() { 
     //Create Point object (calls Point class constructor) 
     Point point = new Point(1.0f,2.0f); 

     //Check x is set inside the created Point object 
     assertEquals(1.0f, point.get_x()); 

     //Check y is set inside the created Point object 
     assertEquals(2.0f, point.get_y()); 
    }