白盒测试junit实现三角形问题

白盒测试junit实现三角形问题

Triangle类

public class Triangle {

private int a,b,c;
public Triangle(int a,int b,int c){
	this.a=a;
	this.b=b;
	this.c=c;
}
public String isTriangle(Triangle triangle){
	String s;
	if(a<0 || b<0 || c<0) {
		s="边长为不能为负数和零";
	} else if(a>=b+c || b>=a+c || c>=a+b){
		s="不是三角形";
	} else {
		if (a==b && c==b) {				
			s="是等边三角形";
		} else {
			if (a==b || b==c || c==a) {
				s="是等腰三角形";		
			} else {
				s="是不规则三角形";		
			}
		}		
	}		
	System.out.println(a+" "+b+" "+c+s);
	return s;
}

}

TriangleTest类

import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class TriangleTest {

private int p1;
private int p2;
private int p3;
private String expected_triangle="";
@Parameters
public static Collection date(){ 
	//将所有测试数据放在object数组对象中 
	//object数组对象可使用任意数据类型
	Object[][] object= {
		//语句覆盖
		{-1,2,3,"边长为不能为负数和零"},
		{1,2,9,"不是三角形"},
		{2,2,3,"是等腰三角形"},			
		{1,1,1,"是等边三角形"},
		{3,4,5,"是不规则三角形"},	
	};
	return Arrays.asList(object);
}	 
public TriangleTest(int p1,int p2,int p3,String expect_triangle){
	this.p1=p1;
	this.p2=p2;
	this.p3=p3;
	this.expected_triangle=expect_triangle;
}
@Test
public void test_istriangle(){
	Triangle triangle=new Triangle(p1,p2,p3);	
	String result_triangle=triangle.isTriangle(triangle);
	assertEquals(expected_triangle,result_triangle);
}

}

你可能感兴趣的:(白盒测试)