測試驅動編程和持續集成部署應該說是現在軟件開發者的必備武器,不過跟其他很多好東西一樣,在我們公司的推廣總要慢上一拍,畢竟老板看的是你能夠把功能實現好讓客戶滿意,所以能不折騰就不折騰。但不管用不用,先學下單元測試技術吧。
JUnit單元測試
用JUnit寫單元測試,首先是定義一個單元測試類;然后在類中定義測試方法,使用@Test標簽即可。
重要標簽
ANNOTATION | DESCRIPTION |
---|---|
@Test | 定義test case |
@Test(expected = Exception.class) | 如果沒有拋出某個異常則失敗 |
@Test(timeout=100) | 如果超過多少毫秒則失敗 |
@Before | 在每個test case前執行 |
@After | 在每個test case后執行 |
@BeforeClass | 在每個test class前執行 |
@AfterClass | 在每個test class執行 |
@Ignore | 忽略執行某個測試方法 |
運行多個測試集
可以定義一個測試集來依次運行多個測試類
1 package com.vogella.junit.first; 2 3 import org.junit.runner.RunWith; 4 import org.junit.runners.Suite; 5 import org.junit.runners.Suite.SuiteClasses; 6 7 @RunWith(Suite.class) 8 @SuiteClasses({ MyClassTest.class, MySecondClassTest.class }) 9 public class AllTests { 10 11 }
JMockit使用
單元測試一般只測試某一個功能,但是由於類之間的耦合性往往難以把功能隔離開來。例如你希望測試某個業務邏輯處理數據的功能,但是數據是從Database取回的,這就涉及到DAO層的類調用;你不希望單元測試函數去訪問數據庫(除非是測試DAO的單元測試),於是你希望有一個假的DAO類剛好返回你需要的測試數據。Mock的作用就是在單元測試里模擬類的行為和狀態。市面上有好幾種Mock庫,包括EasyMock, Mockit等,這里我還是推薦功能更加強大的JMockit庫。
JMockit有好幾種不同的使用方式,但基本的思路相同。第一就是需要用到哪些Mock類或者方法,就定義多少,絕不Mock無用的類和方法。第二在編寫程序時候就需要盡量使得類之間的調用接口化。
第一種是所謂的"State-oriented mocking",也就是基於狀態的模擬,有個例子:
1 package com.test; 2 3 import mockit.MockUp; 4 5 import org.junit.Test; 6 import junit.framework.TestCase; 7 8 /** 9 * 類名: 10 * 類說明: 11 * 12 * @version V1.0 13 * @author lu7kang 2011-3-31 14 */ 15 public class MyTestCase extends TestCase { 16 17 @Test 18 public void test() throws Exception{ 19 // mock第三方接口方法-返回成功 20 MockUp mu3 = new MockUp() { 21 @Mock 22 public EFaxResult updateNumberProfile(NumberProfile numberProfile, String memberId) { 23 EFaxResult rst = new EFaxResult(); 24 // mock第三方方法-返回成功100 25 rst.setResultCode(ResultCode.RC_100); 26 return rst; 27 } 28 }; 29 // 測試本地代碼 30 MyService myservice = new MyServiceImpl(); 31 NumberProfile numberProfile = new NumberProfile(); 32 String memberId = "test_id"; 33 // 測試自己的方法 34 rst = myservice.doMyNumberProfile(numberProfile, memberId); 35 Assert.assertEquals(ResultCode.RC_100, rst.getResultCode()); 36 } 37 }
在MyService的實現里用到了ESPService的updateNumberProfile方法,於是測試中通過創建一個new MockUp()類(並實現updateNumberProfile方法),可以讓JMockit在單元測試運行過程中用Mock的updateNumberProfile代替原生的調用。
還有一種Behavior Based使用方法,其定義mock的方式就是先錄制好某些方法調用和返回結果。例如:
1 2 @Mocked T mock1; // all current and future instances are mocked 3 @Injectable K anotherMock; // only one particular instance is mocked 4 5 @Test 6 public void aTestMethod() 7 { 8 new NonStrictExpectations() {{ 9 mock1.getData(); result = "my test data"; 10 anotherMock.doSomething(anyInt, "some expected value", anyString); times = 1; 11 }}; 12 13 // In the replay phase, the tested method would call the "getData" and "doSomething" 14 // methods on a "MyCollaborator" instance. 15 ... 16 17 // In the verify phase, we may optionally verify expected invocations to "MyCollaborator" 18 // objects. 19 ... 20 }