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.https://baike.baidu.com/item/junit/1211849?fr=ge_ala)
Body mass index (BMI) calculation: BMI = weight/height squared (kg/㎡)
Classification
BMI value (kg/㎡)
Thin
[0, 18.5)
normal
[18.5, 24)
Fat
[24, 28)
Obesity
[28, + ∞)
table>
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.
//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.