利用Junit对BMI指数进行单元测试

本文介绍了如何在Java项目中使用Junit4进行单元测试,包括设置权重、身高并计算BMI,以及使用注解如@Before、@After等进行测试准备和清理。同时展示了构造器注入和属性注入的参数化测试用例,以及分类测试和测试套件的运用。
摘要由CSDN通过智能技术生成

本次测试是对于Junit测试平台的实践使用。Junit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个,Junit有自己的扩展生态圈。多数Java的开发环境都已经集成了Junit作为单元测试的工具。它是一个回归测试框架,Junit测试是程序员测试,即所谓白盒测试。Junit是开源的Java测试框架,用于编写和运行可重复的测试,是用于单元测试框架体系xUnit的一个实例。有4种特性:用于测试期望结果的断言(Assertion);用于共享共同测试数据的测试工具;用于方便的组织和运行测试的测试套件;图形和文本的测试运行器。

本次测试中使用的是Junit4,比之前的版本功能性要强,但是比最新版本的Junit5还是会有些不足之处。

(Junit资料介绍源自百度百科-验证百度百科是一部内容开放、自由的网络百科全书,旨在创造一个涵盖所有领域知识,服务所有互联网用户的中文知识性百科全书。在这里你可以参与词条编辑,分享贡献你的知识。icon-default.png?t=N7T8https://baike.baidu.com/item/junit/1211849?fr=ge_ala

身体质量指数BMI计算:BMI = 体重 / 身高的平方(kg/㎡)

分类BMI值(kg/㎡)
偏瘦[0, 18.5)

正常

[18.5, 24)

偏胖

[24, 28)
肥胖[28, +∞) 

1、使用Junit中常见注解

package sample.ut;

public class BMI {
    private double weight; //体重
    private double height; //身高

    //Getter and Setter
    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    //一次性设置体重和身高
    public void setParams(double w, double h) {
        this.weight = w;
        this.height = h;
    }

    //定义构造
    public BMI(double w, double h) {
        this.weight = w;
        this.height = h;
    }

    public BMI() {
        this.weight = 0.0;
        this.height = 0.0;
    }

    //定义功能方法,计算BMI,并判断所属分类
    public String getBMIType() {
        String result = "";
        double bmi = 0.0;

        if (weight > 0 && height > 0) {
            bmi = weight / (height * height);
            if (bmi < 18.5) {
                result = "偏瘦";
            }else if (bmi < 24) {
                result = "正常";
            }else if (bmi < 28) {
                result = "偏胖";
            }else {
                result = "肥胖";
            }
        }else {
            result = "数据有误";
        }
        return result;
    }
}

package sample.ut;

import org.junit.*;
import static org.junit.Assert.*;

public class BMITest {
    BMI testObj; //被测类

    @Before
    public void setUp() throws Exception {
        testObj = new BMI();
        System.out.println("Run @Before method");
    }

    @After
    public void tearDown() throws Exception {
        testObj = null;
        System.out.println("Run @After method");
    }

    @BeforeClass
    public static void prepareEnvironment() {
        System.out.println("Run @BeforeClass method");
    }

    @AfterClass
    public static void RestoreEnvironment() {
        System.out.println("Run @AfterClass method");
    }

    @Test
    public void getBMIType_Thin() {
        System.out.println("Run getBMIType_Thin method");
        testObj.setParams(45.0, 1.6);
        String excepted = "偏瘦";
        assertTrue(testObj.getBMIType()==excepted);
    }

    @Test
    public void getBMIType_Normal() {
        System.out.println("Run getBMIType_Normal method");
        testObj.setParams(55.0, 1.6);
        String excepted = "正常";
        assertTrue(testObj.getBMIType()==excepted);
    }

    @Test
    public void getBMIType_SlightlyFat() {
        System.out.println("Run getBMIType_SlightlyFat method");
        testObj = new BMI(68.0, 1.6);
        String excepted = "偏胖";
        assertTrue(testObj.getBMIType()==excepted);
        testObj = null;
    }

    @Ignore
    @Test
    public void getBMIType_Fat() {
        System.out.println("Run getBMIType_Fat method");
        testObj = new BMI(80.0, 1.6);
        String excepted = "肥胖";
        assertTrue(testObj.getBMIType()==excepted);
        testObj = null;
    }
}

【结果截图】

2、使用参数化运行器@Runwith(Parameterized.class) 分别实现构造器注入和属性注入两种参数注入。

//构造注入
@RunWith(Parameterized.class)
public class BMITest {
    BMI testObj; //被测类
    private double weight;   //体重
    private double height;   //身高
    private String expected; //预期分类

    public BMITest(double w, double h, String exp) {
        this.weight = w;
        this.height = h;
        this.expected = exp;
    }

    @Parameterized.Parameters(name = "{index}:getBMIType[{0},{1}] = [{2}]")
    public static Collection testDataset() {
        return Arrays.asList(
                new Object[][]{
                        {45.0, 1.6, "偏瘦"},
                        {55.0, 1.6, "正常"},
                        {68.0, 1.6, "偏胖"},
                        {80.0, 1.6, "肥胖"}
                }
        );
    }

    @Before
    public void setUp() throws Exception {
        testObj = new BMI(weight, height);
        //System.out.println("Run @Before method");
    }

    @After
    public void tearDown() throws Exception {
        testObj = null;
        //System.out.println("Run @After method");
    }

    @Test
    public void getBMIType() {
        assertTrue(testObj.getBMIType() == expected);
    }

【结果截图】

//属性注入
@RunWith(Parameterized.class)
public class BMITest {
    BMI testObj; //被测类

    @Parameterized.Parameter(0)
    public double weight;   //体重

    @Parameterized.Parameter(1)
    public double height;   //身高

    @Parameterized.Parameter(2)
    public String expected; //预期分类

    @Parameterized.Parameters(name = "{index}:getBMIType[{0},{1}] = [{2}]")
    public static Collection testDataset() {
        return Arrays.asList(
                new Object[][]{
                        {45.0, 1.6, "偏瘦"},
                        {55.0, 1.6, "正常"},
                        {68.0, 1.6, "偏胖"},
                        {80.0, 1.6, "肥胖"}
                }
        );
    }

    @Before
    public void setUp() throws Exception {
        testObj = new BMI(weight, height);
        //System.out.println("Run @Before method");
    }

    @After
    public void tearDown() throws Exception {
        testObj = null;
        //System.out.println("Run @After method");
    }

    @Test
    public void getBMIType() {
        assertTrue(testObj.getBMIType() == expected);
    }

【结果截图】

3、使用套包运行器@Runwith(Suite.class) 实现两个被测单元的测试用例统一管理。

package sample.ut;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ BMITest.class, BloodPressureTest.class})
public class TestSuiteWithBMIAndBloodPressure {

}

【结果截图】

4、使用分类运行器@Runwiht(Categories.class) 实现两个被测单元的对某些特定的测试用例进行分类测试。

package sample.ut;

import org.junit.*;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.*;

public class BMITest {
    BMI testObj; //被测类

    @Before
    public void setUp() throws Exception {
        testObj = new BMI();
        //System.out.println("Run @Before method");
    }

    @After
    public void tearDown() throws Exception {
        testObj = null;
        //System.out.println("Run @After method");
    }

    @Test
    public void getBMIType_Thin() {
        //System.out.println("Run getBMIType_Thin method");
        testObj.setParams(45.0, 1.6);
        String excepted = "偏瘦";
        assertTrue(testObj.getBMIType()==excepted);
    }

    @Test
    public void getBMIType_Normal() {
        //System.out.println("Run getBMIType_Normal method");
        testObj.setParams(55.0, 1.6);
        String excepted = "正常";
        assertTrue(testObj.getBMIType()==excepted);
    }

    @Test
    public void getBMIType_SlightlyFat() {
        //System.out.println("Run getBMIType_SlightlyFat method");
        testObj = new BMI(68.0, 1.6);
        String excepted = "偏胖";
        assertTrue(testObj.getBMIType()==excepted);
        testObj = null;
    }

    @Test
    public void getBMIType_Fat() {
        //System.out.println("Run getBMIType_Fat method");
        testObj = new BMI(80.0, 1.6);
        String excepted = "肥胖";
        assertTrue(testObj.getBMIType()==excepted);
        testObj = null;
    }

    @Category({EPTest.class})
    @Test
    public void testGetBMIType_Boundary1() {
        testObj.setParams(47.10, 1.6);
        assertTrue(testObj.getBMIType() == "偏瘦");
    }

    @Category({EPTest.class})
    @Test
    public void testGetBMIType_Boundary2() {
        testObj.setParams(47.37, 1.6);
        assertTrue(testObj.getBMIType() == "正常");
    }

    @Category({EPTest.class})
    @Test
    public void testGetBMIType_Boundary3() {
        testObj.setParams(61.18, 1.6);
        assertTrue(testObj.getBMIType() == "正常");
    }

    @Category({EPTest.class})
    @Test
    public void testGetBMIType_Boundary4() {
        testObj.setParams(61.45, 1.6);
        assertTrue(testObj.getBMIType() == "偏胖");
    }

    @Category({EPTest.class})
    @Test
    public void testGetBMIType_Boundary5() {
        testObj.setParams(71.42, 1.6);
        assertTrue(testObj.getBMIType() == "偏胖");
    }

    @Category({EPTest.class})
    @Test
    public void testGetBMIType_Boundary6() {
        testObj.setParams(71.69, 1.6);
        assertTrue(testObj.getBMIType() == "肥胖");
    }

    @Category({EPTest.class})
    @Test
    public void testGetBMIType_Boundary7() {
        testObj.setParams(71.94, 1.6);
        assertTrue(testObj.getBMIType() == "肥胖");
    }
}

【结果截图】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗜甜兔星人

创作不易,球球各位看官支持一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值