Junit 測試

看了許多文章,都有說junit的重要性。我的不喜歡用這個,由於我仍是比較喜歡寫main方法的,雖然麻煩點,可是是個習慣。如今也寫寫這個,畢竟是頗有用的。框架

代碼拿一下寫框架時候的用的吧,代碼來自園中的某位大嬸。dom

import com.tear.ioc.bean.xml.document.XmlDocumentHolder;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class XmlDocumentHolderTest {
    private XmlDocumentHolder xmlHolder;
    @Before
    public void setUp() throws Exception {
        xmlHolder = new XmlDocumentHolder();
    }

    @After
    public void tearDown() throws Exception {
        xmlHolder = null;
    }

    @Test
    public void testGetDocument1() {
        String filePath = "test/resources/document/xmlDocumentHolder.xml";
        Document doc1 = xmlHolder.getDocument(filePath);
        assertNotNull(doc1);
        Element root = doc1.getRootElement();
        assertEquals(root.getName(), "beans");
        Document doc2 = xmlHolder.getDocument(filePath);
        System.out.println(doc1);
        System.out.println(doc1);
        assertEquals(doc1, doc2);
    }

    @Test(expected = DocumentException.class)
    public void testGetDocument2(){
        String filePath = "test/resources/document/xmlDocumentHolder2.xml";
        Document doc = xmlHolder.getDocument(filePath);
    }

    @Test(expected = DocumentException.class)
    public void testGetDocument3() throws DocumentException{
        String filePath = "test/resources/document/xmlDocumentHolder3.xml";
        Document doc = xmlHolder.getDocument(filePath);
    }
}

這裏少了assertTrue和aseertFalse。引入的包是4的就好的。spa

相關文章
相關標籤/搜索