美文网首页
方法测试,不用主函数,使用junit测试环境进行测试

方法测试,不用主函数,使用junit测试环境进行测试

作者: 熊猫的雄 | 来源:发表于2018-09-05 19:46 被阅读0次

在一个class文件中,我们往往写了很多方法,一般在测试这些方法时我们得在main函数new一个对象来调用这些方法。
今天学习了 @Test 方法进行直接调用方法实现测试的目的。

package com.work;

import org.junit.Test;

public class Method2 {
    @Test
    public void test(){
//      1. 定义一个方法,无返回值,有参数。打印指定 M 行,每行有 N 个 * 的矩形。
//      2. 定义一个方法,有返回值,有参数。录入三个数值,求平均值,并返回。
        rextangle(10,4);
        System.out.println(avg(1,2,3));
    }
    //打印矩形
    public void rextangle(int width,int height){
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    }
    //求平均值
    public double avg(double num1,double num2,double num3){
        double avg = (num1+num2+num3)/3;
        return avg;
    }
}

如上图已经写好了方法rectangle(int width,int height)、avg(double num1,double num2,double num3)
再定义方法test(),在test()上方@Test,eclipse工具中会有如下图的提示

提示.png
点击 Add JUnit 4 library to the build path
会在左边包资源管理器中的项目中自动导入JUnit 4 类库
类库.png
然后就可以在test()方法中直接调用函数或者进行其他测试操作了

相关文章

网友评论

      本文标题:方法测试,不用主函数,使用junit测试环境进行测试

      本文链接:https://www.haomeiwen.com/subject/dtggwftx.html