junit测试

来源:互联网 发布:fd抓包更改数据犯法么 编辑:程序博客网 时间:2024/05/16 19:27

junit测试,可以单独测试,不需要反复注释代码。

package test;



import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


public class Demo2 {
                                                    //在有多个test的时候,可以再beforewClass和before中初始和销毁(after)
                                                    //需要的变量,不需要反复编写
                                                    //beforeClass和before的区别,前者是在整个类的前后执行一次。
                                                    //    before是在每个test的前后都要执行一遍。
    @BeforeClass
    public static void beforeClass(){                //beforeClass 的类需要是静态类。
        System.out.println("初始化");
    }
    
    @Before
    public void before(){
        System.out.println("初始化");
    }
    
    @Test
    public void run(){
        Person p = new Person();
        Object buid = null;                                            //在junit3中assert是junit.framework类中的。在junit4中为下面
        p.run();                                                    //assert是import org.junit.Assert;中的
        Assert.assertArrayEquals(new int[]{1,1}, new int[]{1,1});    //前者为预期值,后者世界收到的值
        Assert.assertEquals("2", "1");                                //多种类型,可以进行数组及各种类型的设置
        Assert.assertNotNull("buid不能为空",buid);                    //如果变量为空,则抛出message
    }
    
    @Test
    public void eat(){
        Person p = new Person();
        p.eat();
        
    }
    
    @After
    public void after(){
        System.out.println("销毁");
    
    }
    
    @AfterClass
    public static void afterClass(){
        System.out.println("销毁");
    }
}



0 0
原创粉丝点击