本人在初步學到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();
}
}