為什麼進行單元測試?
1. 重用測試, 應付将來實作的變化.
2. 明确指定我的東西是沒問題的.
Failure, error的差別?
Failure隻測試失敗, Error指程式本身出錯
1. new ->java project: JUnit4
2. src右鍵->new->class T, package: com.bjsxt.junit4
package com.bjsxt.junit4;
public class T {
public int add(int x, int y){
return x+y;
}
public int divide(int x, int y){
return x/y;
}
}
3. 包com.bjsxt.junit4 右鍵-> new->package-> com.bjsxt.junit4.test ->右鍵-> new->JUnit test case(New JUnit 4 test, name: TTest, class under test:T)->next -> 選擇要測試的T方法->finish->選擇預設的myeclipse 的jar 包,
4. build path->remove ... 删除原生jar, 添加自己下載下傳的jar, build-> add external archieves.
5. 打開TTest.java,編寫測試代碼:
package com.bjsxt.unit4.test;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import org.junit.Test;
import com.bjsxt.u2.T;
public class TTest {
@Test
public void testAdd() {
int z = new T().add(5,3);
assertThat(z,is(8));
assertThat(z, allOf(greaterThan(1), lessThan(10)));
}
}
keep bar green to keep bugs clean.
如果不先删除包, 會出現如下錯誤:
1. 使用上述is方法時提示錯誤, 需要引入hamcrest jar包(core和license包): 右鍵工程-> build path ,引入兩個包
然後發現出現:java.lang.SecurityException: class "org.hamcrest.Matchers"‘s signer information does not match signer information of other classes in the same package
因為hamcrest的包和junit4裡的hamcrest包的classloader用的不是同一個.解決方法:
把myeclipse自帶的包都去掉, 引入自己的包junit即可.
如果測試divide時出現異常怎麼辦?
在annotation裡加入expected的異常類型即可. 後面timeout是時間的判斷
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
int z=new T().divide(5,0);
}
Annotation:
1. @Test : 測試方法
2. @Ignore: 被忽略的測試方法
3. @Before: 每個測試方法之前運作 用法: 執行方法前打開某個檔案
4. @After: 每個測試方法之後運作 用法: 執行方法後關閉某個檔案
5. @BeforeClass: 所有測試方法之前運作 用法: 配置工作, 建立資料庫連接配接等費時工作
6. @AfterClass: 所有測試方法之後運作 用法: 比如關閉資料庫
例子:
package com.bjsxt.junit4.test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.bjsxt.junit4.T;
public class TTest {
@BeforeClass //提前配置工作, 比如資料庫連接配接等耗費時間的資源, 搭載複雜環境時
public static void beforeClass(){
System.out.println("BeforeClass");
}
@AfterClass //關閉資料庫連接配接
public static void afterClass(){
System.out.println("AfterClass");
}
@Before //打開檔案使用
public void before(){
System.out.println("before");
}
@Test
public void testAdd() {
int z=new T().add(5,3);
assertThat(z,is(8));
//assertTrue("z is not bigger than 3",z>3);
}
[email protected]
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
int z=new T().divide(5,0);
}
@After //關閉檔案使用
public void after(){
System.out.println("after");
}
}
結果:
BeforeClass
before
after
before
after
AfterClass
還可以建立另一個類, User:
package com.bjsxt.junit4;
public class User {
public String getName(){
return "songhuiqiao";
}
}
在test包右鍵, 建立unit test, UserTest
package com.bjsxt.junit4.test;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.is;
import org.junit.Test;
import com.bjsxt.junit4.User;
public class UserTest {
@Test
public void testGetName() {
assertThat(new User().getName(),is("songhuiqiao"));
}
}
如果兩個類同時運作的話, 在test包右鍵, run->configuration->run all:
原文:http://www.cnblogs.com/wujixing/p/5403399.html