实验内容:
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”