• JUnit test用法


     JUnit主要是用来测试白盒的,比方你写了几个函数想想测试它是否执行正常,使用方式如下:

    1.首先新建一个项目叫JUnit_Test,我们编写一个Calculator类,这是一个能够简单实现加减乘除、平方、开方的计算器类,然后对这些功能进行单元测试

    package JUnit_Test;
    
    
    
    public class Calculator {
    
        private static int result; // 静态变量,用于存储运行结果
    
        public static void add(int n) {
    
            result = result + n;
    
        }
    
        public void substract(int n){
    
            result = result - 1;  //Bug: 正确的应该是 result =result-n
    
        }
    
        public void multiply(int n){
    
        }         // 此方法尚未写好
    
        public void divide(int n){
    
            result = result / n;
    
        }
    
        public void square(int n){
    
            result = n * n;
    
        }
    
        public void squareRoot(int n){
    
            for (; ;) ;            //Bug : 死循环
    
        }
    
        public void clear(){     // 将结果清零
    
            result = 0;
    
        }
    
        public static int getResult(){
    
            return result;
    
        }
    
    }

    2.第二步,将JUnit4单元测试包引入这个项目:在该项目上点右键,点“属性”,在弹出的属性窗口中,首先在左边选择“Java Build Path”,然后到右上选择“Libraries”标签,之后在最右边点击“Add Library…”按钮

    3.第三步,生成JUnit测试框架:在Eclipse的Package Explorer中用右键点击该类弹出菜单,选择“New à JUnit Test Case”,可以选择你要测试的方法,新建完成之后会自动生成一个新类CalculatorTest,里面包含一些空的测试用例。你只需要将这些测试用例稍作修改即可使用。完整的CalculatorTest代码如下:

    package JUnit_Test;
    
    import static org.junit.Assert.*;
    
    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    public class CalculatorTest {
    
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
        }
    
        @AfterClass
        public static void tearDownAfterClass() throws Exception {
        }
    
        @Before
        public void setUp() throws Exception {
        }
    
        @After
        public void tearDown() throws Exception {
        }
    
        @Test
        public void testAdd() {
            Calculator.add(4);
            Calculator.add(5);
            assertEquals(9, Calculator.getResult());
        }
    
        @Test
        public void testSubstract() {
            fail("Not yet implemented");
        }
    
        @Test
        public void testMultiply() {
            fail("Not yet implemented");
        }
    
        @Test
        public void testDivide() {
            fail("Not yet implemented");
        }
    
    }

    4.第四步,运行测试代码:按照上述代码修改完毕后,我们在CalculatorTest类上点右键,选择“Run As à JUnit Test”来运行我们的测试,结果会按P、F来显示

    备注:使用junit时必须导入的包

    (1)在测试类中用到了JUnit4框架,自然要把相应地Package包含进来。最主要地一个Package就是org.junit.*。把它包含进来之后,绝大部分功能就有了。

    (2)还有一句话也非常地重要“import static org.junit.Assert.*;”,我们在测试的时候使用的一系列assertEquals方法就来自这个包。

    (3)大家注意一下,这是一个静态包含(static),是JDK5中新增添的一个功能。也就是说,assertEquals是Assert类中的一系列的静态方法,一般的使用方式是Assert. assertEquals(),但是使用了静态包含后,前面的类名就可以省略了,使用起来更加的方便。

  • 相关阅读:
    MacOS更改zsh命令行前缀
    python中os._exit()和sys.exit(), exit(0)和exit(1) 的用法和区别
    如何解析 redis 的 rdb 文件
    流量回放工具<二>
    策略路由配置<一>
    h3c镜像模式配置
    python上传gz文件请求
    优先队列(大顶堆实现)
    bm和kmp和bf
    dedecms 软件下载模块加入flashget快车下载代码
  • 原文地址:https://www.cnblogs.com/penghong2014/p/4305500.html
Copyright © 2020-2023  润新知