《实验3 白盒法-Junit进阶使用》

实验内容:

1.对上个实验的“三角形类(Triangle)对三角形形状判断”进行修改,增加成员函数void setTriangle(long lbordera, long lborderb, long lborderc) 来设置三角形的三条边,并对测试代码进行优化重构。
2.将“非三角形(Illegal)”作为异常抛出,使用JUnit来处理异常。
3.在三角形类的测试类里体验测试生命周期方法。
4.使用参数化测试完成“三角形类(Triangle)对三角形形状判断”,

代码:

Triangle :

package com.linyanbin.softTestStudy.exp2;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
 * @author linyanbin
 * @date 2023/3/3 11:39
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Triangle {
    protected long a=0;
    protected long b=0;
    protected long c=0;
    public void setTriangle(long a, long b, long c) throws Exception {
        if (!isTriangle(a, b, c)) {
            throw new Exception("Illegal");
        }
        this.a = a;
        this.b = b;
        this.c = c;
    }
    /**
     * 检测是否三角形
     * @return boolean
     **/
    public boolean isTriangle(long a, long b, long c) {
        return ((a + b) > c) && ((a + c) > b) && ((b + c) > a);
    }
    /**
     * 检测是否等腰三角形
     * @return boolean
     **/
    public boolean isIsosceles() {
        return a == b || a == c || b == c;
    }
    /**
     * 检测是否等边三角形
     * @return boolean
     **/
    public boolean isRegular() {
        return a == b && a == c;
    }
    /**
     * 检测三角形形状,返回:非三角形(Illegal)、一般三角形(Scalene)、等腰三角形(Isosceles) 、等边三角形(Regular)
     * @return java.lang.String
     **/
    public String getType() {
        if (isRegular()) {
            return "Regular";
        }
        if (isIsosceles()) {
            return "Isosceles";
        }
        return "Scalene";
    }

    /**
     * 计算两边之差的绝对值
     * @Param [a, b]
     * @return long
     **/
    public long difOfBorders(long a, long b) {
        return Math.abs(a - b);
    }
}

TriangleTest:

package com.linyanbin.softTestStudy.exp2;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
/**
 * @author linyanbin
 * @date 2023/3/3 12:10
 */
@RunWith(Parameterized.class)
public class TriangleTest {
    private final long a;
    private final long b;
    private final long c;
    private final String expected;
    private static Triangle triangle;
    public TriangleTest(long a, long b, long c, String expected) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.expected = expected;
    }
    @BeforeClass
    public static void BeforeClass() {
        System.out.println("测试开始");
    }
    @AfterClass
    public static void afterClass() {
        System.out.println("测试结束");
    }
    @Before
    public void setUp() {
        triangle = new Triangle();
    }
    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                { 3, 4, 5, "Scalene" },
                { 2, 2, 3, "Isosceles" },
                { 1, 1, 1, "Regular" },
                { 1, 2, 3, "Illegal" }
        });
    }
    @Test
    public void getType(){
        try {
            triangle.setTriangle(a, b, c);
            Assert.assertEquals(expected, triangle.getType());
            if ("Illegal".equals(triangle.getType())) {
                Assert.fail("Expected an exception to be thrown");
            }
        } catch (Exception e) {
            Assert.assertEquals("Illegal", e.getMessage());
        }
    }
    @Test
    public void testSetTriangle() {
        Triangle triangle = new Triangle();
        try {
            triangle.setTriangle(1, 2, 3);
            Assert.fail("Expected an exception to be thrown");
        } catch (Exception e) {
            Assert.assertEquals("Illegal", e.getMessage());
        }
    }
    @Test
    public void testScaleneTriangle() throws Exception {
        // 测试一般三角形
        triangle.setTriangle(3, 4, 5);
        String type = triangle.getType();
        Assert.assertEquals("Scalene", type);
    }
    @Test
    public void testIsoscelesTriangle() throws Exception {
        // 测试等腰三角形
        triangle.setTriangle(3, 3, 4);
        String type = triangle.getType();
        Assert.assertEquals("Isosceles", type);
    }
    @Test
    public void testRegularTriangle() throws Exception {
        // 测试等边三角形
        triangle.setTriangle(1, 1, 1);
        String type = triangle.getType();
        Assert.assertEquals("Regular", type);
    }
}

测试样例:

3, 4, 5, “Scalene”
2, 2, 3, “Isosceles”
1, 1, 1, “Regular”
1, 2, 3, “Illegal”

运行结果:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值