软件测试一:利用 Junit 实现单元测试
一、 问题及代码
1、 问题
前业利桑那州境内的一位步枪销售商销售密苏里州制造商制造的步枪机
(lock),枪托(stock)和枪管(barrel),枪机卖 45 美元,枪托卖 30 美元,
枪管卖 25 美元,销售商每月至少要售出一支完整的步枪。且生产限额是大多数
销售商在一个月内可销售 70 个枪机、80 个枪托和 90 个枪管,每访问一个镇子
之后,销售商都给密苏里州步枪制造商发出电报,说明在哪个镇子中售出的枪机、
枪托和枪管数量。到了月末,销售商要发出一封很短的电报。通知-1 个枪击被售
出。这样步枪制造商就知道当月的销售情况,并计算销售商的佣金如下:销售额
不到(含)1000 美元的部分为 10%,1000(不含)-1800(含)美元的部分为 15%,
超过 1800 美元的部分为 20%。佣金程序生成月份销售报告,汇总售出的枪机,
枪托和枪管总数,销售上的总销售额以及佣金。
2、 代码
//Salary.java
package main;
import java.util.Scanner;
public class Salary {
private int lockPrice=45;
//枪机的价格
private int stockPrice=30; //枪托的价格
private int barrelPrice=25; //枪管的价格
private int locks;
private int stocks;
private int barrels;
private double totalPrice;
private double commission;
public Salary(int clocks,int cstocks,int cbarrels){
locks=clocks;
stocks=cstocks;
barrels=cbarrels;
}
/*public double getTotalPrice(){
return totalPrice;
}
public double getCommission(){
return commission;
}*/
public double totalPrice() {
if(locks>=1&&locks<=70&&stocks>=1&&stocks<=80&&barrels>=1&&barrels<=90)
{
totalPrice = locks * lockPrice + stocks * stockPrice + barrels *
barrelPrice;
System.out.println("销售金额为:" + totalPrice + "美元");
return totalPrice;
}
else
System.out.println("输入有错误!!!");
return -1;
}
public double commission(){
if(totalPrice>0) {
if (totalPrice > 1800) {
commission = (totalPrice - 1800) * 0.2 + 800 * 0.15 + 1000 * 0.1;
} else if (totalPrice > 1000) {
commission = (totalPrice - 1000) * 0.15 + 1000 * 0.1;
} else {
commission = totalPrice * 0.1;
}
System.out.println("佣金为:" + commission + "美元");
return commission;
}
else
System.out.println("输入有错误!!!");
return -1;
}
public boolean equals(double etotalPrice,double ecommission,Object object) {//
判断钱数是否相同
if (object instanceof Salary) {
Salary salary = (Salary) object;
return salary.totalPrice() == etotalPrice && salary.commission() ==
ecommission;
}
return false;
}
public static void main(String args[]){
int locks;
int stocks;
int barrels;
Scanner in =new Scanner(System.in);
System.out.println("请输入枪机数量:");
locks=in.nextInt();
System.out.println("请输入枪托数量:");
stocks=in.nextInt();
System.out.println("请输入枪管数量:");
barrels=in.nextInt();
Salary salary=new Salary(locks,stocks,barrels);
System.out.println(salary.totalPrice());
System.out.println(salary.commission());
// System.out.println(salary.equals(1800,220,salary));
}
二、 测试用例
1、在此次问题中,我采用了边界值测试方法,因此,分析如下:
根据上图可以找出在不越界的情况下,佣金的最大,最小值。以及一些中点,可
帮助我更好地找出合适的测试用例。
2. 根据上图,可得出以下测试用例:
用例 ID
枪机
枪托
枪管
销售额
1
2
3
4
1
1
1
2
1
1
2
1
1
2
1
1
100
125
130
145
佣金
10
12.5
13
14.5
注释
输出最小
值
输出略大
于最小值
输出略大
于最小值
输出略大
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
5
10
10
9
10
10
10
11
14
18
18
17
18
18
18
19
48
70
70
69
70
5
10
9
10
10
10
11
10
14
18
17
18
18
18
19
18
48
80
79
80
80
5
9
10
10
10
11
10
10
14
17
18
18
18
19
18
18
48
89
90
90
90
500
975
970
955
1000
1025
1030
1045
1400
1775
1770
1755
1800
1825
1830
1845
4800
7775
7770
7755
7800
50
97.5
97
95.5
100
103.75
104.5
106.75
160
216.25
215.5
213.25
220
225
226
229
820
1415
1414
1411
1420
于最小值
中点
略低于边
界点
略低于边
界点
略低于边
界点
边界点
略高于边
界点
略高于边
界点
略高于边
界点
中点
略低于边
界点
略低于边
界点
略低于边
界点
边界点
略高于边
界点
略高于边
界点
略高于边
界点
中点
输出略小
于最大值
输出略小
于最大值
输出略小
于最大值
输出最大
值
26
27
70
0
81
80
90
90
输入错误 输入错误
越界
输入错误 输入错误 不满足条
件
三、 IDEA 中 Junit 单元测试搭建方法
1、 settings->Plugins->搜索安装
2、 下载对应版本的 Junit,并在 Project Structure 中添加模块 Junit
3、 新建测试项目
Crtl+shift+T 到如下步骤:
3、填对应信息,点击 OK
四、 测试代码及运行结果
1、测试代码
//SalaryTest.java
package main;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Scanner;
import static org.junit.Assert.*;
public class SalaryTest extends TestCase {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testTotalSalary() {
int locks=10;
int stocks=20;
int barrels=30;
/* Scanner in =new Scanner(System.in);
System.out.println("请输入枪机数量:");
locks=in.nextInt();
System.out.println("请输入枪托数量:");
stocks=in.nextInt();
System.out.println("请输入枪管数量:");
barrels=in.nextInt();*/
Salary salary=new Salary(locks,stocks,barrels);
//System.out.println(salary.totalPrice());
//System.out.println(salary.commission());
/* Salary salary=new Salary(17,19,21);
Salary expected=new Salary(17,19,21);*/
Assert.assertTrue(salary.equals(1800,220,salary));//判断运行结果是否与预期
的相同
}
}
2、运行结果
(1)
(2)