Dashboard > OpenSource Project > ... > JUnit > JUnit Intro - Assert methods
OpenSource Project Log In   View a printable version of the current page.
JUnit Intro - Assert methods
Added by zach14c, last edited by koji lin on Sep 27, 2004  (view change)
Labels: 
(None)

Reference by JavaWorld, caterpillar


  • Part I
    Assert 提供一組判斷結果的方法(assert methods),像是assertXXXX()等,這些方法都是static method,它們會在斷定結果不一致時丟出一個實作Throwable的物件,當中會帶有失敗的訊息。
    我們先撰寫一個簡單的Student類別,以利用它來進行一些測試:
    Student.java
    public class Student
    {
        private String _number;
        private String _name;
        private int _score;
    
        public Student()
        {
            _number = null;
            _name = null;
            _score = 0;
        }
    
        public Student(String num, String name, int score)
        {
            _number = num;
            _name = name;
            _score = score;
        }
    
        public String getNumber()
        {
            return _number;
        }
    
        public String getName()
        {
            return _name;
        }
    
        public int getScore()
        {
            return _score;
        }
    
        public void setNumber(String num)
        {
            _number = num;
        }
    
        public void setName(String name)
        {
            _name = name;
        }
    
        public void setScore(int score)
        {
            _score = score;
        }
    }


    我們先來看看在不使用JUnit的情況下,我們如何撰寫一個測試表達式(Test Expression)以測試我們的類別撰寫無誤,例如您可能是這麼撰寫的:

    Test Expression without JUnit
    public class Test2
    {
        public static void main(String[] args)
        {
            Student student = new Student("B83503124", "Justin", 100);
    
            if (!((student.getName() != null) &&
                    student.getName().equals("Justin")))
            {
                System.out.println("getName method failure");
            }
        }
    }


    同樣一個功能,如果我們使用Assert提供的斷言方法,則可以如下撰寫:

    Test Expression without JUnit
    import junit.framework.*;
    
    public class Test3
    {
        public static void main(String[] args)
        {
            Student student = new Student("B83503124", "Justin", 100);
            Assert.assertEquals("getName metohd failure", "Justin",
                student.getName());
        }
    }

    Assert被歸類於junit.framework中;如果斷言失敗,我們會收到一個Throwable物件,有這個例子中會是ComparisonFailure物件,例如我們故意讓以上斷言失敗的話,可能收到以下的訊息:

     
    Exception in thread "main" junit.framework.ComparisonFailure: 
      getName metohd failure expected:<Justin> but was:<B83503124> 
        at junit.framework.Assert.assertEquals(Assert.java:81) 
        at Test3.main(Test3.java:7)
    


    如果您不想收到這些冗長的訊息,您可以使用ComparisonFailure的getMessage()方法取得您指定的錯誤顯示訊息,像是上面的"getName method failure"。


  • Part 2
    assertEquals()提供因應各種資料型態的多型,像是int、String等等,如果是比較物件的話,則根據equals()方法傳回的true或false來比較,所以在您想要比較兩個物件的field是否相同時,您要重新定義您的equals()方法,例如在我們的Student中,我們的equals()可以定義如下:
    Determine object is equal
    public boolean equals(Object anObject)
        {
            if (anObject instanceof Student)
            {
                Student aStudent = (Student) anObject;
    
                return aStudent.getNumber().equals(getNumber()) &&
                (aStudent.getName() == getName()) &&
                (aStudent.getScore() == getScore());
            }
    
            return false;
        }


    傳統的測試表達式可以這麼撰寫:

    Test object is equal without JUnit
    Student student1 = new Student("B83503124", "Justin", 100);
            Student student2 = new Student("B83503124", "Justin", 100);
    
            if (!student1.equals(student2))
            {
                System.out.println("instances  are unequal");
            }


    使用Assert的assertEquals()則可以這麼撰寫:

    Test object is equal with JUnit
    Student student1 = new Student("B83503124", "Justin", 100);
            Student student2 = new Student("B83503124", "Justin", 100);
            Assert.assertEquals("instances  are unequal", student1, student2);



  • Part III
    比較兩個物件內容是否相同,通常是測試時很常進行的一個動作,而Assert類別也提供了像是assertTure()、assertFalse()、assertSame() 、assertNotSame()、assertNull()、assertNotNull()等等的方法,以便利各種測試場合的需要,例如我們上面的測試也可以這麼撰寫:
    Use assertTrue method
    Student student1 = new Student("B83503124", "Justin", 100);
    Student student2 = new Student("B83503124", "Justin", 100);
    Assert.assertTure(student1.equals(student2));

    在JUnit中會很常使用到Assert中的各種方法,但以上只是單一個Assert類別的幾個方法介紹,還談不上是一個測試,只要將各個測試進行組合,才談的上是真正的測試開始。

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.1.5a Build:#411 Mar 16, 2006) - Bug/feature request - Contact Administrators