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);
        }
    
    
    }
  • 相关阅读:
    事件处理之跨浏览器
    IE事件处理
    DOM0级事件处理、DOM2级事件处理
    JS内置对象学习总结
    JS事件响应的学习总结
    vuex的学习例子
    npm run build 打包后,如何运行在本地查看效果(Apache服务)
    Vue.js 引入外部js方法
    Table展开行
    正则表达式test()和exec()、 search() 和 replace()用法实例
  • 原文地址:https://www.cnblogs.com/daixianjun/p/junit.html
Copyright © 2011-2022 走看看