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);
        }
    
    
    }
  • 相关阅读:
    Es spring data jpa 修改连接配置
    object转map类型
    记一次项目中yaml文档引发的惨案 (#yaml文档格式#yaml中'-'的作用)
    02-方法重载
    01-数据类型分类
    Centos 7 Sublime 安装 package control
    修改AdminLTE左侧菜单展开延迟
    【Flask】Flask Restful api
    【Flask】Flask常用信号
    【Flask】Flask上下文
  • 原文地址:https://www.cnblogs.com/daixianjun/p/junit.html
Copyright © 2011-2022 走看看