Using Junit to unit test the BMI index

This test is a practical use of the Junit test platform. Junit is a unit testing framework for Java language. It was founded by Kent Beck and Erich Gamma and has gradually become the most successful one in the xUnit family derived from Kent Beck’s sUnit. Junit has its own extended ecosystem. Most Java development environments have integrated Junit as a unit testing tool. It is a regression testing framework, and Junit testing is programmer testing, the so-called white-box testing. Junit is an open source Java testing framework for writing and running repeatable tests. It is an instance of xUnit, a unit testing framework system. There are 4 features: assertions for testing expected results; test tools for sharing common test data; test suites for convenient organization and running of tests; graphical and text test runners.

Junit4 was used in this test, which is more functional than the previous version, but still has some shortcomings compared to the latest version of Junit5.

(Junit information introduction comes from Baidu Encyclopedia – verified Baidu Encyclopedia is an open and free online encyclopedia, aiming to create a Chinese knowledge encyclopedia covering all fields of knowledge and serving all Internet users. Here you can participate in the word Article editor, share and contribute your knowledge.icon-default.png?t=N7T8https://baike.baidu.com/item/junit/1211849?fr=ge_ala)

Body mass index (BMI) calculation: BMI = weight/height squared (kg/㎡)

1. Use common annotations in Junit

package sample.ut;

public class BMI {
    private double weight; //weight
    private double height; //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;
    }

    //Set weight and height at once
    public void setParams(double w, double h) {
        this.weight = w;
        this.height = h;
    }

    //Define structure
    public BMI(double w, double h) {
        this.weight = w;
        this.height = h;
    }

    publicBMI() {
        this.weight = 0.0;
        this.height = 0.0;
    }

    //Define functional methods, calculate BMI, and determine the category it belongs to
    public String getBMIType() {
        String result = "";
        double bmi = 0.0;

        if (weight > 0 & amp; & amp; height > 0) {
            bmi = weight / (height * height);
            if (bmi < 18.5) {
                result = "thin";
            }else if (bmi < 24) {
                result = "normal";
            }else if (bmi < 28) {
                result = "fat";
            }else {
                result = "obesity";
            }
        }else {
            result = "Data is incorrect";
        }
        return result;
    }
}

package sample.ut;

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

public class BMITest {
    BMI testObj; //Tested class

    @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 = "thin";
        assertTrue(testObj.getBMIType()==excepted);
    }

    @Test
    public void getBMIType_Normal() {
        System.out.println("Run getBMIType_Normal method");
        testObj.setParams(55.0, 1.6);
        String excepted = "Normal";
        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 = "Fat";
        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 = "Obesity";
        assertTrue(testObj.getBMIType()==excepted);
        testObj = null;
    }
}

[Screenshot of results]

2. Use the parameterized runner @Runwith(Parameterized.class) to implement two types of parameter injection: constructor injection and property injection.

//Construction injection
@RunWith(Parameterized.class)
public class BMITest {
    BMI testObj; //Tested class
    private double weight; //weight
    private double height; //height
    private String expected; //expected classification

    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, "thin"},
                        {55.0, 1.6, "Normal"},
                        {68.0, 1.6, "Fat"},
                        {80.0, 1.6, "obesity"}
                }
        );
    }

    @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);
    }

[Screenshot of results]

//Attribute injection
@RunWith(Parameterized.class)
public class BMITest {
    BMI testObj; //Tested class

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

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

    @Parameterized.Parameter(2)
    public String expected; //expected classification

    @Parameterized.Parameters(name = "{index}:getBMIType[{0},{1}] = [{2}]")
    public static Collection testDataset() {
        return Arrays.asList(
                new Object[][]{
                        {45.0, 1.6, "thin"},
                        {55.0, 1.6, "Normal"},
                        {68.0, 1.6, "Fat"},
                        {80.0, 1.6, "obesity"}
                }
        );
    }

    @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);
    }

[Screenshot of results]

3. Use the package runner @Runwith(Suite.class) to achieve unified management of test cases for the two units under test.

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 {

}

[Screenshot of results]

4. Use the classification runner @Runwiht(Categories.class) to implement classification testing of certain specific test cases between the two units under test.

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; //Tested class

    @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 = "thin";
        assertTrue(testObj.getBMIType()==excepted);
    }

    @Test
    public void getBMIType_Normal() {
        //System.out.println("Run getBMIType_Normal method");
        testObj.setParams(55.0, 1.6);
        String excepted = "Normal";
        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 = "Fat";
        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 = "Obesity";
        assertTrue(testObj.getBMIType()==excepted);
        testObj = null;
    }

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

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

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

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

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

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

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

[Screenshot of results]

syntaxbug.com © 2021 All Rights Reserved.
Classification BMI value (kg/㎡)
Thin [0, 18.5)

normal

[18.5, 24)

Fat

[24, 28)
Obesity [28, + ∞)