zoukankan      html  css  js  c++  java
  • 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);
        }
    
    
    }
  • 相关阅读:
    vue教程2-06 过滤器
    vue教程2-05 v-for循环 重复数据无法添加问题 加track-by='索引'
    vue教程2-04 vue实例简单方法
    Linux文件I/O
    Linux内存管理
    进程中内存地址空间的划分
    Ubuntu12.04 15.04禁止移动介质自动播放
    条件编译,头文件,静态库,共享库与多文件编程
    C语言的函数
    C语言流程控制
  • 原文地址:https://www.cnblogs.com/daixianjun/p/junit.html
Copyright © 2011-2022 走看看