BMI例題如下:
BMI中國計算標準:體質指數(BMI)=體重(kg)÷身高^2(m)
例如:一個人的身高為1.75米,體重為68千克,他的BMI=68/(1.75^2)=22.2(千克/米^2)當BMI指數為18.5~23.9時屬正常。
成人的BMI數值標準:
過輕:低于18.5???????? 正常:>=18.5且<24
過重:>=24且<28??????? 肥胖:>=28且<32
非常肥胖:>=32
第1步:在IEDA環境下完成BMI類的代碼實現
方案1:通過鍵盤輸入身高,體重或者直接通過構造方法或者BMI類的成員方法初始化身高體重,然后調用方法計算BMI值,并人工判斷校驗。
package sample;
import java.util.Scanner;
import java.text.DecimalFormat;
import static java.lang.Math.abs;
public class BMI {double height; //身高double weight; //體重//設置和得到屬性值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 BMI(double w, double h) {weight = w;height = h;}//設置體重和身高public void setParams(double w, double h) {weight = w;height = h;}
//根據 BMI 值判斷健康狀況public String getBMIType() {double bmi = 0.0;String result = "";
//設置浮點數輸出格式,保留 2 位小數DecimalFormat df = new DecimalFormat("#.00");if (weight > 0 && height > 0) {
//計算 BMIbmi = weight / (height * height);
//2、根據 bmi 判斷所屬健康分類if (bmi < 18.5) {result = "偏瘦";} else if (bmi < 24) {result = "正常";} else if (bmi < 28) {result = "過重";}else if (bmi < 32) {result = "肥胖";}else {result = "非常肥胖";} } else {return "重量或者身高錯誤!";}System.out.println("bmi 的值是:" + df.format(bmi));return result;}public static void main(String[] args) {
//方案 1
//用戶輸入體重和身高,調用被測方法,結果輸出到屏幕
//得到一個掃描對象,從鍵盤接收數據Scanner reader = new Scanner(System.in);double w = 0.0, h = 0.0;System.out.println("請輸入體重(公斤)和身高(米),以等號=結束");//檢測到下一個數為 Double 類型,則返回 Truewhile (reader.hasNextDouble()) {w = reader.nextDouble();h = reader.nextDouble();}
BMI testobj = new BMI(w, h);String result = testobj.getBMIType();String output = "體重:" + w + ",身高:" + h + ",BMI 狀況是:" + result;System.out.println(output);//設置多個測試用例BMI tmpobj = new BMI(45.0, 1.6);String type = tmpobj.getBMIType();System.out.println(type);tmpobj.setParams(55, 1.6);System.out.println(tmpobj.getBMIType());tmpobj.setParams(68, 1.6);System.out.println(tmpobj.getBMIType());tmpobj.setParams(80, 1.6);System.out.println(tmpobj.getBMIType());}
}
根據自身實際問題再做修改。
第2步:針對BMI類設計測試用例
輸入 | BMI值 | 等價類/邊界值 | 預期輸出 | ||
用例編號 | 體重(KG) | 身高(M) | |||
1 | 59.95 | 1.80 | 18.5 | 等于邊界值18.5 | 正常 |
2 | 46.24 | 1.70 | 16 | 輸出等價類小于18.5 | 過輕 |
3 | 0 | 1.70 | 輸入體重邊界值0 | 輸入有誤 | |
4 | 48.91 | 1.62 | 18.6 | 輸出等價類大于18.5 | 正常 |
5 | 69.12 | 1.70 | 23.9 | 輸出等價類小于24 | 正常 |
6 | 68.55 | 1.69 | 24.0 | 等于邊界值24 | 過重 |
7 | 71.32 | 1.72 | 24.1 | 輸出等價類大于24 | 過重 |
8 | 82.56 | 1.72 | 27.9 | 輸出等價類小于28 | 過重 |
9 | 79.10 | 1.68 | 28.0 | 等于邊界值28 | 肥胖 |
10 | 80.31 | 1.69 | 28.1 | 輸出等價類大于28 | 肥胖 |
11 | 93.31 | 1.71 | 31.9 | 輸出等價類小于32 | 肥胖 |
12 | 88.20 | 1.66 | 32.0 | 等于邊界值32 | 非常肥胖 |
13 | 88.50 | 1.66 | 32.1 | 輸出等價類大于32 | 非常肥胖 |
14 | 100.04 | 1.69 | 35.0 | 輸出等價類大于32 | 非常肥胖 |
15 | 60.00 | 0 | 輸入身高邊界值0 | 輸入有誤 | |
16 | 200.00 | 1.7 | 69.2 | 無效等價類超出正常體重 | 輸入有誤 |
17 | 60.00 | 2.50 | 9.6 | 無效等價類超出正常身高 | 輸入有誤 |
18 | 60.00 | 0.90 | 74.1 | 無效等價類低于正常身高 | 輸入有誤 |
設計用例不多,可以再自行增加邊界值用例。
第3步
方案2是在方案1的基礎上改進,將預期值和計算的BMI值進行比較,實現自動校驗。
package sample;
import java.util.Scanner;
import java.text.DecimalFormat;
import static java.lang.Math.abs;
public class BMI {double height; //身高double weight; //體重//設置和得到屬性值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 BMI(double w, double h) {weight = w;height = h;}//設置體重和身高public void setParams(double w, double h) {weight = w;height = h;}
//根據 BMI 值判斷健康狀況public String getBMIType() {double bmi = 0.0;String result = "";
//設置浮點數輸出格式,保留 2 位小數DecimalFormat df = new DecimalFormat("#.00");if (weight > 0 && height > 0) {
//計算 BMIbmi = weight / (height * height);
//2、根據 bmi 判斷所屬健康分類if (bmi < 18.5) {result = "偏瘦";} else if (bmi < 24) {result = "正常";} else if (bmi < 28) {result = "過重";}else if (bmi < 32) {result = "肥胖";}else {result = "非常肥胖";} } else {return "重量或者身高錯誤!";}System.out.println("bmi 的值是:" + df.format(bmi));return result;}public static void main(String[] args) {
//方案 2:腳本自行根據測試用例來設置體重和身高,并自動校驗執行結果
//1、創建被測對象
BMI testobj=new BMI(48.91,1.62);//2、調用被測方法String actual=testobj.getBMIType();//3、校驗執行結果String expected="正常";String output="";
if(actual==expected){ output+="pass";}else{ output+="Fail,體重:48.91,身高 1.62,Expected:"+expected+",Actual:"+actual;}output+="\n";
//測試用例 2
testobj.setParams(69.12,1.70); actual=testobj.getBMIType(); expected="正常";
if(actual==expected){output+="pass";}else{ output+="Fail,體重:69.12,身高 1.70,Expected:"+expected+",Actual:"+actual;}output+="\n";
//測試用例 3
testobj.setParams(68.55,1.69); actual=testobj.getBMIType(); expected="過重";
if(actual==expected){ output+="pass";}else{ output+="Fail,體重:68.55,身高 1.69,Expected:"+expected+",Actual:"+actual;}output+="\n";
//測試用例 4
testobj.setParams(71.32,1.72); actual=testobj.getBMIType(); expected="過重";
if(actual==expected){ output+="pass";}else{ output+="Fail,體重:71.32,身高 1.72,Expected:"+expected+",Actual:"+actual;}output+="\n";
//4、輸出結果
System.out.println(output);}
}
第4步
先另外創建一個TestBMI類,在方案1和方案2基礎上做如下改進:
方案3代碼如下:
package sample;
import static java.lang.Math.abs;
class TestBMI {BMI bmiObj; //被測類//創建被測對象public void createTestobj(double w, double h) {bmiObj = new BMI(w, h);}//釋放被測對象public void freeTestobj() {bmiObj = null;}
//執行結果校驗public boolean verify(String expected, String actual) {if (expected == actual) { return true;} else {return false;}}//記錄執行過程public String record(double w, double h, String expected, String actual, boolean testResult) {String output = "";if (testResult) { output += "Pass. 體重:" + w + ", 身高:" + h;} else {output += "Fail. 體重:" + w + ", 身高:" + h +", Expected:" + expected + ", Actual:" + actual;}return output;}//測試用例 1public void testGetBMIType1() { createTestobj(48.91, 1.62);String actual = bmiObj.getBMIType();boolean testResult = verify("正常", actual);System.out.println(record(48.91, 1.62, "正常", actual, testResult));freeTestobj();}//測試用例 2public void testGetBMIType2() { createTestobj(69.12, 1.70);String actual = bmiObj.getBMIType();boolean testResult = verify("正常", actual);System.out.println(record(69.12, 1.70, "正常", actual, testResult));freeTestobj();}//測試用例 3public void testGetBMIType3() { createTestobj(68.55, 1.69);String actual = bmiObj.getBMIType();boolean testResult = verify("過重", actual);System.out.println(record(68.55, 1.69, "過重", actual, testResult));freeTestobj();}
//測試用例 14public void testGetBMIType4() { createTestobj(71.32, 1.72);String actual = bmiObj.getBMIType();boolean testResult = verify("過重", actual);System.out.println(record(71.32, 1.72, "過重", actual, testResult));freeTestobj();}//主函數public static void main(String[] args) {TestBMI test = new TestBMI();test.testGetBMIType1();test.testGetBMIType2();test.testGetBMIType3();test.testGetBMIType4();}
}
第5步:BMI類下創建BMITest類進行獨立測試
測試結果如下:
代碼如下:
package sample;
import org.junit.Test;
import static org.junit.Assert.*;
public class BMITest {BMI testobj; //創建被測類@Testpublic void getBMIType() {
//創建被測對象testobj=new BMI(48.91,1.62);String expected="正常";
//System.out.println(testobj.getBMIType());
//調用測試方法,并校驗測試結果assertTrue(testobj.getBMIType()==expected);testobj=null;}@Testpublic void getBMIType_Normal() {
//創建被測對象testobj=new BMI(69.12,1.70);String expected="正常";
//調用測試方法,并校驗測試結果assertTrue(testobj.getBMIType()==expected);testobj=null;}@Testpublic void getBMIType_Thin() {
//創建被測對象testobj=new BMI(68.55,1.69);String expected="過重";
//調用測試方法,并校驗測試結果assertTrue(testobj.getBMIType()==expected);testobj=null;
}
@Testpublic void getBMIType_SlightlyFat() {
//創建被測對象
testobj=new BMI(71.32,1.72);String expected="過重";
//調用測試方法,并校驗測試結果assertTrue(testobj.getBMIType()==expected);
//釋放對象testobj=null;}@Testpublic void getBMIType_Fat() {
//創建被測對象testobj=new BMI(79.1,1.68);String expected="肥胖";
//調用測試方法,并校驗測試結果assertTrue(testobj.getBMIType()==expected);testobj=null;}
}
第6步:BMI類創建BMITest1類,使用Before和After方法進行獨立測試
測試結果如下:
代碼如下:
package sample;
import org.junit.*;
import static org.junit.Assert.*;
public class BMITest1 {BMI testobj;@Beforepublic void setUp() {System.out.println("Run @Before method");testobj = new BMI();}@Afterpublic void tearDown() {System.out.println("Run @After method");testobj = null;}@BeforeClasspublic static void prepareEnvironment() {System.out.println("Run @BeforeClass Method");}@AfterClasspublic static void RestoreEnvironment() {System.out.println("Run @AfterClass Method");}@Testpublic void getBMIType() {
//創建被測對象testobj.setParams(55.0, 1.6);String expected = "正常";
//System.out.println(testobj.getBMIType());
//調用測試方法,并校驗測試結果assertTrue(testobj.getBMIType() == expected);
}
@Testpublic void getBMIType_Normal() {
//賦值被測對象testobj.setParams(55.0, 1.6);String expected = "正常";
//調用測試方法,并校驗測試結果assertTrue(testobj.getBMIType() == expected);}
@Testpublic void getBMIType_Thin() {
//賦值被測對象testobj.setParams(45.0, 1.6);String expected = "偏瘦";
//調用測試方法,并校驗測試結果assertTrue(testobj.getBMIType() == expected);}@Testpublic void getBMIType_SlightlyFat() {
//賦值被測對象testobj.setParams(55.0, 1.6);String expected = "正常";
//調用測試方法,并校驗測試結果assertTrue(testobj.getBMIType() == expected);}@Testpublic void getBMIType_Fat() {
//賦值被測對象testobj.setParams(80.0, 1.6);String expected = "肥胖";
//調用測試方法,并校驗測試結果assertTrue(testobj.getBMIType() == expected);}
}
代碼根據需求或用例自行修改。