1. 程式人生 > >Junit4 三角形測試例子,供初學者借鑑(僅供參考)

Junit4 三角形測試例子,供初學者借鑑(僅供參考)

本人在初步學到Junit4 時,用簡單的三角形作為demo,以下是詳細程式碼:

Triangle.java 檔案 程式碼

/**
 * 三角形問題實現
 * 給定三條邊,判斷三角形的形狀
 * @author zhouxin
 *
 */
public class Triangle {
	private int side1;
	private int side2;
	private int side3;
	
	public Triangle()
	{
		this(1,1,1);
	}
	
	public Triangle(int a,int b,int c)
	{
		this.side1 = a;
		this.side2 = b;
		this.side3 = c;
	}
	
	/**
	 * 獲取三角形型別
	 * @return
	 * @throws Exception
	 */
	public String GetTriangleType() throws Exception
	{
		if(side1<1||side1>100||side2<1||side2>100||side3<1||side3>100)
			return "邊取值超出範圍";
		if(side1+side2<=side3||side1+side3<=side2||side2+side3<=side1)
			throw new Exception("不構成三角形");
		if(side1==side2&&side2==side3)//等邊三角形
			return "等邊三角形";
		else if(side1==side2||side2==side3||side1==side3)//等腰三角形
		{
			return "等腰三角形";
		}
		else if(IsRtTriangle(side1,side2,side3))
			return "直角三角形";
		else//一般三角形
			return "一般三角形";
	}
	
	/**
	 * 判斷是否為直角三角形
	 * @param a
	 * @param b
	 * @param c
	 * @return
	 */
	private boolean IsRtTriangle(int a,int b,int c)
	{
		int a_2 = a*a;
		int b_2 = b*b;
		int c_2 = c*c;
		if(a_2+b_2==c_2||a_2+c_2==b_2||b_2+c_2==a_2)
			return true;
		return false;
	}

	public static void main(String[] args) {
    	Triangle tr = new Triangle(11,11,11);
    	try
    	{
    		System.out.println(tr.GetTriangleType());
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    }
}

TriangleTest.java 檔案程式碼:

import static org.junit.Assert.*;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/** 
 * 引數化測試的類必須有Parameterized測試執行器修飾 
 * 
 */ 

//step3 類的引數化註釋
@RunWith(Parameterized.class)
public class TriangleTest {
	private Triangle t;
	private int s1;
	private int s2;
	private int s3;
	private String exp_type;
	private boolean excepted; 

	//step4 寫新的傳參構造方法
	public TriangleTest(int s1,int s2,int s3,boolean excepted,String type) 
			throws Exception{
		this.s1 = s1;
		this.s2 = s2;
		this.s3 = s3;
		this.excepted = excepted;
		this.exp_type = type;
	}
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		t = new Triangle(s1,s2,s3);
	}

	@After
	public void tearDown() throws Exception {
	}
	//step2 
	/* 準備測試資料。資料的準備需要在一個方法中進行,該方法需要滿足一定的要求: 

        1)該方法必須由Parameters註解修飾 
        2)該方法必須為public static的 
        3)該方法必須返回Collection型別 
        4)該方法的名字不做要求 
        5)該方法沒有引數 
	 * @return 
	 */  
	/*將所有測試資料放在object陣列物件中 
	 object陣列物件可使用任意資料型別
	    實際測試時 自己加入更多的測試資料*/
	@Parameters
	public static Collection data(){
		Object[][] object = {
				{-1,2,3,false,"邊取值超出範圍"},
				{101,99,99,false,"邊取值超出範圍"},
				{1,-1,3,false,"邊取值超出範圍"},
				{99,101,99,false,"邊取值超出範圍"},
				{1,2,-1,false,"邊取值超出範圍"},
				{99,99,101,false,"邊取值超出範圍"},
				{2,2,3,false,"等腰三角形"},
				{2,3,2,false,"等腰三角形"},
				{3,2,2,false,"等腰三角形"},
				{2,2,2,false,"等邊三角形"},
				{3,4,5,true,"直角三角形"},
				{4,3,5,true,"直角三角形"},
				{5,3,4,true,"直角三角形"},
				{4,5,3,true,"直角三角形"},
				{2,3,4,false,"一般三角形"},
				{3,2,4,false,"一般三角形"},
				{4,2,3,false,"一般三角形"},
				{3,4,2,false,"一般三角形"},
				};
		return Arrays.asList(object);

	}
	//step1  做一個公用測試方法
	//三角形的型別
	@Test
	public void testGetTriangleType() throws Exception {

		String result_type = t.GetTriangleType();

		assertEquals(exp_type,result_type);
	}

	//是否為直角三角形 測試Triangle.java中的私有方法
	@Test
	public void testIsRtTriangle() throws Exception {
		//獲取Class 物件
		Class triangle = t.getClass();
		//獲取方法
		Method method = triangle.getDeclaredMethod("IsRtTriangle", 
				new Class[]{int.class,int.class,int.class});
		//將私有設定可訪問
		method.setAccessible(true);
		//傳值,返回結果物件
		Object actual = method.invoke(t,s1,s2,s3);
		//比較預期和結果
		assertEquals(excepted,actual);
	}


}

TriangleTestException.java 程式碼如下
import static org.junit.Assert.*;

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

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/*
 * 測試Triangle.java中異常
 */
//step3 類的引數化註釋
@RunWith(Parameterized.class)
public class TriangleTestException {
	private Triangle t;
	private int s1;
	private int s2;
	private int s3;
	private String exp_type;
	private boolean excepted;
	
	//step4 寫新的傳參構造方法
	public TriangleTestException(int s1,int s2,int s3,boolean excepted,String type) 
			throws Exception{
		this.s1 = s1;
		this.s2 = s2;
		this.s3 = s3;
		this.excepted = excepted;
		this.exp_type = type;
	}

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		t = new Triangle(s1,s2,s3);
	}

	@After
	public void tearDown() throws Exception {
	}
	//step2
	@Parameters
	public static Collection data(){
		Object[][] object = {
				{1,2,3,false,"不構成三角形"},
				{2,3,1,false,"不構成三角形"},
				{3,1,2,false,"不構成三角形"}};
				
		return Arrays.asList(object);
		
	}
  /**
   * 三條邊構不成三角形而丟擲異常
   * 公用測試方法
   * @throws Exception
   */
	//step1
	@Test(expected=Exception.class)
	public void testGetTriangleTypeException() throws Exception{
		t.GetTriangleType();
		
	}

}