java的Junit单元测试

函数主要分为以下几类:

1.有固定返回值的。用assert 方法即可。

2.修改了状态。

(1)修改了数据库中的数据。可以查询数据库(select  语句),看数据是否发生了改变。

    --原则上应该是用伪造数据的方法解决这种依赖。

 两个小例子:

import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ChannelManagerImplTest {
    @Test
    public void Test(){
        assertEquals(2,1+5);
    }
}

有很多个参数的:

mport static org.junit.Assert.*;

import java.io.IOException;
import java.util.List;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import com.google.common.collect.Lists;

@RunWith(Parameterized.class)
public class ChannelManagerImplTest {
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Parameters
    public static List<Object[]> dataFeed() {
        return Lists.asList(new Object[]{-1, 1, 0}, new Object[][]{{20, 20, 40},{30, 30, 60},{-5, -5, -10}});
    }
    @Parameter(value = 0)
    public int o1;
    @Parameter(value = 1)
    public int o2;
    @Parameter(value = 2)
    public int expector;

    @Test
    public void test() throws IOException, RuntimeException{
        assertEquals(expector, o1 + o2);
        System.out.println("o1+o2 := " + (o1 + o2));
        System.out.println("expector := " + expector);
    }


}
原文地址:https://www.cnblogs.com/daixianjun/p/junit.html