• 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);
        }
    
    
    }
  • 相关阅读:
    实验4-1-5 韩信点兵 (10分)
    实验4-1-6 求分数序列前N项和 (15分)
    实验7-1-5 选择法排序 (20分)
    实验7-1-2 求最大值及其下标 (20分)
    第一次个人编程作业
    3.Vue.js-目录结构
    2.VUEJS-安装
    1.Vuejs-第一个实例
    Mybatis通用Mapper介绍与使用
    商城项目团购之定时任务2
  • 原文地址:https://www.cnblogs.com/daixianjun/p/junit.html
Copyright © 2020-2023  润新知