Junit4参数化测试(三角形、NextDate函数问题、佣金问题)

好久没有写博客了,最近都在复习考研。碰上软件测试课程,作业要求用Junit4参数化测试代码,记录一下子。

一、三角形问题

题目内容

输入3个整数a、b和c分别作为三角形的三条边,要求a、b和c必须满足以下条件:
1、整数
2、3个数
3、边长大于等于1,小于等于100
4、任意两边之和大于第三边
输出为5种情况之一:
如果不满足条件1、2、3,则程序输出为“输入错误”。
如果不满足条件4,则程序输出为“非三角形”。
如果三条边相等,则程序输出为“等边三角形”。
如果恰好有两条边相等,则程序输出为“等腰三角形”。
如果三条边都不相等,则程序输出为“一般三角形”。

实现代码:
package com.ctgu.zyj.work1;

public class Triangle {

    //判断输入object是否为整数
    public static boolean isInteger(Object object){
        if (object instanceof Integer){
            return true;
        }else {
            return false;
        }
    }

    //判断三角形两边之和是否大于第三边
    public boolean judgeTriangle(int a,int b,int c){
        if (a+b>c&&b+c>a&&a+c>b){
            return true;
        }else {
            return false;
        }
    }

    //判断是否为等腰三角形
    public boolean strightLine(int a,int b,int c){
        if ((a==b&&b!=c)||(b==c&&c!=a)||(a==c&&c!=b)){
            return true;
        }else {
            return false;
        }
    }

    //判断输出
    public String isTriangle(Object... objects){
        //2.判断是否为3个数
        int len = objects.length;
        if (len!=3) return "输入错误";
        //1.判断object是否为整数
        for (Object object:objects){
                if (!isInteger(object)){
                    return "输入错误";
                }
        }
        //1,3.判断所有边长是否都大于等于1,小于等于100、
        String[] sides = new String[3];
        for (int i = 0;i<objects.length;i++){
             sides[i] = objects[i].toString();
             Integer temp = Integer.valueOf(sides[i]);
            if ((temp < 1 || temp > 100)){
                return "输入错误";
            }
        }
        int a = Integer.valueOf(sides[0]);
        int b = Integer.valueOf(sides[1]);
        int c = Integer.valueOf(sides[2]);
        if (!judgeTriangle(a,b,c)){
            return "非三角形";
        }
        if (a == b && b ==c){
            return "等边三角形";
        }
        if (strightLine(a,b,c)){
            return "等腰三角形";
        }
        return "一般三角形";
    }
}

测试代码:
package com.ctgu.zyj.work1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class TriangleTest {
    Triangle triangle = new Triangle();

    public String expected;
    public Object[] sides = null;

    public TriangleTest(String expected,Object[] sides){
        this.expected = expected;
        this.sides = sides;
    }

    @Parameterized.Parameters
    public static Collection<Object[]> date() {
        return Arrays.asList(new Object[][]{
                {"等腰三角形", new Object[]{50,50,1}},//边界值测试
                {"等腰三角形", new Object[]{50,50,2}},
                {"等边三角形", new Object[]{50,50,50}},
                {"等腰三角形", new Object[]{50,50,99}},
                {"非三角形", new Object[]{50,50,100}},
                {"等腰三角形", new Object[]{50,1,50}},//
                {"等腰三角形", new Object[]{50,2,50}},
                {"等腰三角形", new Object[]{50,99,50}},
                {"非三角形", new Object[]{50,100,50}},
                {"等腰三角形", new Object[]{1,50,50}},//
                {"等腰三角形", new Object[]{2,50,50}},
                {"等腰三角形", new Object[]{99,50,50}},
                {"非三角形", new Object[]{100,50,50}},

        }
        );
    }

    @Test
    public void isTriangle() {
        assertEquals(expected,triangle.isTriangle(sides));
    }
}

二、NextDate函数问题

题目内容

NextDate函数输入为month(月份)、day(日期)和year(年),输出为输入后一天的日期。例如,如果输入为:1964年8月16日,则输出为1964年8月17日。要求输入变量month、day和year都是整数值,并且满足以下条件:
Con1. 1≤month≤12
Con2. 1≤day≤31
Con3. 1900≤year≤2050

实现代码:
package com.ctgu.zyj.work1;

public class NextDate {

    public String nextDate(String line){
        //处理字符串
        line = line.replace('年',',');
        line = line.replace('月',',');
        line = line.replace('日',',');
        String[] str = line.split(",");
        if (str.length<3){
            return "日期输入错误";
        }
        Integer year = Integer.valueOf(str[0]);
        Integer month = Integer.valueOf(str[1]);
        Integer day = Integer.valueOf(str[2]);
        if (year < 1964 || year>2050) {
            return "日期输入错误";
        }

        if (month < 1 || month > 12) {
            return "日期输入错误";
        }

        if (month == 12 && day == 31 ) {
            if (year == 2050){
                return "日期输入错误";
            }else {
                return (year + 1) + "年1月1日";
            }
        }

        // 判断输入的年份是平年还是闰年,来判断当输入2月时天数是否有误
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {// 该年是闰年,2月有29天
            if (month == 2) {
                if (day > 29) {
                    return "日期输入错误";
                } else if (day < 1) {
                    return "日期输入错误";
                } else {
                    if (day == 29) {
                        return year + "年" + 3 + "月" + 1+"日";
                    } else {
                        day += 1;
                    }
                }

            }
        } else {// 该年是平年,2月有28天
            if (month == 2) {
                if (day > 28) {
                    return "日期输入错误";
                } else if (day < 1) {
                    return "日期输入错误";
                } else {
                    if (day == 28) {
                        return year + "年" + 3 + "月" + 1+"日";
                    } else {
                        day += 1;
                    }
                }
            }
        }

        // 判断4 6 9 11 月的天数输入
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            if (day > 30) {
                return "日期输入错误";
            } else if (day < 1) {
                return "日期输入错误";
            } else {
                if (day == 30) {
                    month += 1;
                    day = 1;
                } else {
                    day += 1;
                }
            }
        } else if (month != 2) { // 判断1 3 5 7 8 10 12月的天数输入
            if (day > 31) {
                return "日期输入错误";
            } else if (day < 1) {
                return "日期输入错误";
            } else {
                if (day == 31) {
                    month += 1;
                    day = 1;
                } else {
                    day += 1;
                }
            }
        }

        return year+"年"+month+"月"+day+"日";
    }
}


测试代码:
package com.ctgu.zyj.work1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class NextDateTest {

    public String expected;
    public String line;
    NextDate nextDate = new NextDate();
    public NextDateTest(String expected,String line){
        this.expected = expected;
        this.line = line;
    }

    @Parameterized.Parameters
    public static Collection<String[]> date(){
        return Arrays.asList(new String[][]{//简化后的决策表测试用例表
                {"2001年6月17日","2001年6月16日"},
                {"2001年7月1日","2001年6月30日"},
                {"日期输入错误","2001年6月31日"},
                {"2001年1月17日","2001年1月16日"},
                {"2001年2月1日","2001年1月31日"},
                {"2001年12月17日","2001年12月16日"},
                {"2002年1月1日","2001年12月31日"},
                {"2001年2月17日","2001年2月16日"},
                {"2004年2月29日","2004年2月28日"},
                {"2001年3月1日","2001年2月28日"},
                {"2004年3月1日","2004年2月29日"},
                {"日期输入错误","2001年2月29日"},
                {"日期输入错误","2001年2月30日"}
        });
    }

    @Test
    public void nextDate() {
        assertEquals(expected,nextDate.nextDate(line));
    }
}

三、佣金问题

题目内容

从前有一位销售人员在亚利桑那州代销密苏里军械制造厂生产的步枪配件,包括枪(lock)、枪托(stock)和枪管(barrel)。枪机售价45美元,枪托售价30美元,枪管售价25美元。销售人员每个月至少要卖出一个枪机,一个枪托和一个枪管(但是没有必要是一支完整的步枪),而制造厂的生产能力限制销售人员一个月最多只能卖出70个枪机、80个枪托和90个枪管。每走访过一个城镇之后,销售人员都要给密苏里军械厂发一封电报,汇报在这一城镇中销售枪机、枪托和枪管的数量。销售人员月末会再发一封很短的电报,通知“-1个枪机售出”。这样军械厂就知道当月的销售活动已经结束了,计算销售人员应得的佣金了。佣金计算方法如下:销售总额1000美元以下(含1000美元)部分的佣金为10%,1000至1800美元之间部分的佣金为15%,超过1800美元的部分的佣金为20%。
讨论:
这个佣金程序可分为三个部分:输入数据处理部分,验证输入数据的有效性,销售额统计计算部分,以及佣金计算部分。此处可省略了对输入数据有效性的验证,可使用条件循环语句While来模拟对电报的处理。

实现代码:
package com.ctgu.zyj.work1;

public class CommissionProblem {
    
    //输入数据处理部分
    public Integer[] inputDate(Integer[][] nums){
        Integer[] sum = {0,0,0};
        for (int i = 0;i<nums.length;i++){
            sum[0] += nums[i][0];
            sum[1] += nums[i][1];
            sum[2] += nums[i][2];
        }
        return sum;
    }

    public boolean checkDate(Integer[] sum){
        if (sum[0] <= 70 && sum[0]>=1 && sum[1]<=80 && sum[1]>=1 && sum[2]<=90 &&sum[2]>=1){
            return true;
        }else {
            return false;
        }
    }

    //销售额的统计计算部分,lock:45元,stock:30美元,barrel:25美元
    public String sumCommission(Integer[][] nums){
        Integer[] sum = inputDate(nums);
        double commission = 0;
        if (checkDate(sum)){
            double money = 1.0*(sum[0]*45+sum[1]*30+sum[2]*25);
//            System.out.println(money);
            if (money>1800){
                commission = (money - 1800)*0.2 + 800*0.15 + 1000*0.1;
            }else if (money>1000){
                commission = (money - 1000)*0.15+1000*0.1;
            }else {
                commission = money*0.1;
            }
            return "佣金为:"+commission+"美元";
        }else{
            return "输入数据不合理!";
        }
    }

//    public static void main(String[] args) {
//        System.out.println(new CommissionProblem().sumCommission(new Integer[][]{{5,20,30},{20,30,40}}));
//    }
}

测试代码
package com.ctgu.zyj.work1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class CommissionProblemTest {

    public String expected;
    public Integer[][] nums;

    public CommissionProblemTest(String expected,Integer[][] nums){
        this.expected = expected;
        this.nums = nums;
    }

    @Parameterized.Parameters
    public static Collection<Object[]> date() {
        return Arrays.asList(new Object[][]{
                {"佣金为:735.0美元",new Integer[][]{{5,20,30},{20,30,40}}},//一个月内走访两个城镇
                {"佣金为:359.0美元",new Integer[][]{{5,6,3},{4,8,10},{7,5,6},{9,5,7}}},//一个月内走访4个城镇
                {"输入数据不合理!",new Integer[][]{{0,20,30},{0,30,40}}},//月结枪机售出为0
                {"输入数据不合理!",new Integer[][]{{25,40,45},{40,50,60}}},//月结枪机售出量大于70个
                {"输入数据不合理!",new Integer[][]{{50,50,50}}}//月结枪机售出量大于70个
        });
    }

    @Test
    public void sumCommission() {
        CommissionProblem commissionProblem = new CommissionProblem();
        assertEquals(expected,commissionProblem.sumCommission(nums));
    }
}
  • 15
    点赞
  • 106
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

村头卖假发的小郑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值