Junit 测试

本文介绍了作者对于 JUnit 测试框架的看法及使用经验,并通过一个具体的例子展示了如何使用 JUnit 进行单元测试,包括设置环境、执行测试及断言等步骤。

看了许多文章,都有说junit的重要性。个人不喜欢用这个,因为我还是比较喜欢写main方法的,虽然麻烦点,但是是个习惯。现在也写写这个,毕竟是很有用的。

代码拿一下写框架时候的用的吧,代码来自园中的某位大婶。

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的就好的。

转载于:https://www.cnblogs.com/juepei/p/4134421.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值