本次测试是对于Junit测试平台的实践使用。Junit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个,Junit有自己的扩展生态圈。多数Java的开发环境都已经集成了Junit作为单元测试的工具。它是一个回归测试框架,Junit测试是程序员测试,即所谓白盒测试。Junit是开源的Java测试框架,用于编写和运行可重复的测试,是用于单元测试框架体系xUnit的一个实例。有4种特性:用于测试期望结果的断言(Assertion);用于共享共同测试数据的测试工具;用于方便的组织和运行测试的测试套件;图形和文本的测试运行器。
本次测试中使用的是Junit4,比之前的版本功能性要强,但是比最新版本的Junit5还是会有些不足之处。
身体质量指数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() == "肥胖");
}
}
【结果截图】