JUnit

时间:2021-08-29 20:48:16   收藏:0   阅读:23

Junit单元测试

要测试的类

package com.sgp.main;

public class Calculator {
    public int add(int a, int b){
        return a+b;
    }

    public int sub(int a, int b){
        return a - b;
    }
}

测试类

package com.sgp.test;

import com.sgp.main.Calculator;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class CalculatorTest {
    @Before
    public void init(){
        System.out.println("测试开始了,IO流开始连接");
    }
    @After
    public void close(){
        System.out.println("测试完毕,关闭IO流");
    }
    @Test
    public void testAdd(){
        System.out.println("测试add方法");
        Calculator c = new Calculator();
        int result = c.add(1,2);
        Assert.assertEquals(3,result);
    }
    @Test
    public void testSub(){
        System.out.println("测试sub方法");
        Calculator c = new Calculator();
        int result = c.sub(1,2);
        Assert.assertEquals(result,-1);
    }
}

原文:https://www.cnblogs.com/yellowchives/p/15202187.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!