初步認識JUnitjava
目前大多數的基於Java的企業應用軟件,確定少不了單元測試,程序員經過編寫單元測試來驗證本身程序的有效性;管理者經過持續自動的執行單元測試和分析單元測試覆蓋率來確保軟件自己的質量。能夠說單元測試和集成測試在軟件開發整個流程中佔有舉足輕重的地位。程序員
單元測試,是指對軟件中的最小可測試單元進行檢查和驗證。單元測試不是爲了證實程序是對的,而是證實程序沒有錯。Java經常使用的單元測試框架有JUnit,TestNG,還有些MOCK框架,這裏咱們只來說述JUnit。web
JUnit的兩種版本是JUnit 3.8和JUnit 4,前者使用反射,後者使用反射和註解。spring
1 package com.shop.web.test; 2 public class Calculator { 3 private static double result = 0.0; 4 public void add(double num) { 5 result = result + num; 6 } 7 public void substract(double num) { 8 result = result - num; 9 } 10 public void multiply(double num) { 11 result = result * num; 12 } 13 public void divide(double num) { 14 if (num != 0) { 15 result = result / num; 16 } else { 17 result = result; 18 } 19 } 20 // 清零 21 public void clear() { 22 result = 0; 23 } 24 public double getResult() { 25 return result; 26 } 27 }
CalculatorTest.java數據庫
1 package com.shop.web.test; 2 import junit.framework.TestCase; 3 public class CalculatorTest extends TestCase { 4 private static Calculator calculator = new Calculator(); 5 6 @Override 7 protected void setUp() throws Exception { 8 System.out.println("JUnit initialize the fixture state by overriding setup "); 9 calculator.clear(); 10 } 11 @Override 12 protected void tearDown() throws Exception { 13 System.out.println("JUnit clean-up after a test by overriding tearDown "); 14 calculator.clear(); 15 } 16 public void testAdd() { 17 System.out.println("add result:" + calculator.getResult()); 18 calculator.add(10.1); 19 assertEquals(10.1, calculator.getResult()); 20 } 21 public void testSubstract() { 22 System.out.println("substract result:" + calculator.getResult()); 23 calculator.add(10.1); 24 calculator.substract(2); 25 assertEquals(8.1, calculator.getResult()); 26 } 27 public void testMultiply() { 28 System.out.println("multiply result:" + calculator.getResult()); 29 calculator.add(12); 30 calculator.multiply(12); 31 assertEquals(144.0, calculator.getResult()); 32 } 33 public void testDivide() { 34 System.out.println("divide result:" + calculator.getResult()); 35 calculator.add(12); 36 calculator.divide(12); 37 assertEquals(1.0, calculator.getResult()); 38 } 39 }
綠條表明程序沒有錯誤apache
1 public abstract class TestCase extends Assert implements Test 2 A test case defines the fixture to run multiple tests. 3 To define a test case 4 1) implement a subclass of TestCase 5 2) define instance variables that store the state of the fixture 6 3) initialize the fixture state by overriding setUp 7 4) clean-up after a test by overriding tearDown. 8 Each test runs in its own fixture so there can be no side effects among test runs
1 JUnit initialize the fixture state by overriding setup 2 add result:0.0 3 JUnit clean-up after a test by overriding tearDown 4 JUnit initialize the fixture state by overriding setup 5 substract result:0.0 6 JUnit clean-up after a test by overriding tearDown 7 JUnit initialize the fixture state by overriding setup 8 multiply result:0.0 9 JUnit clean-up after a test by overriding tearDown 10 JUnit initialize the fixture state by overriding setup 11 divide result:0.0 12 JUnit clean-up after a test by overriding tearDown
1 public class CalculatorTest4 { 2 private static Calculator calculator = new Calculator(); 3 4 @Before 5 public void setUp() throws Exception { 6 System.out.println("JUnit initialize the fixture state by overriding setup "); 7 calculator.clear(); 8 } 9 @After 10 public void tearDown() throws Exception { 11 System.out.println("JUnit clean-up after a test by overriding tearDown "); 12 calculator.clear(); 13 } 14 @Test 15 public void add() { 16 System.out.println("add result:" + calculator.getResult()); 17 calculator.add(10.1); 18 assertEquals(10.1, calculator.getResult()); 19 }
四、@Test(expected=*.class)
在JUnit4.0以前,對錯誤的測試,咱們只能經過fail來產生一個錯誤,並在try塊裏面assertTrue(true)來測試。如今,經過@Test元數據中的expected屬性。expected屬性的值是一個異常的類型,用來檢查拋出預期異常。json
1 @Test(expected=ArithmeticException.class) 2 public void divide(){ 3 int i = 2/0; 4 }
五、@Test(timeout=xxx):
該元數據傳入了一個時間(毫秒)給測試方法,
若是測試方法在制定的時間以內沒有運行完,則測試也失敗。mybatis
@Test(timeout=1) public void count(){ for (int i = 0; i < 1000000000; i++) { System.out.println(i); } }
1 @Ignore("此方法如今不須要") 2 @Test 3 public void ignore(){ 4 System.out.println("不須要"); 5 }
Spring整合JUnit app
Junit測試Spring能夠很方便的進行。 框架
用到jar包:spring-test-xxx.jar,junit4的jar。
須要註解 @RunWith、@ContextConfiguration
@RunWith如: @RunWith(SpringJUnit4ClassRunner.class) //表示繼承了SpringJUnit4ClassRunner
@ContextConfiguration如: 用來加載Spring配置文件,@ContextConfiguration(locations = {"classpath:applicationContext-mybatis.xml",……"})
注意:(1)若是spring配置文件applicationContext.xml在classpath路徑下,即一般的src目錄下,這樣加載配置文件,用classpath前綴。
(2)可是在web項目中,有些人喜歡把spring配置文件applicationContext.xml放在WEB-INF目錄下,這裏不是classpath目錄。這種狀況能夠按以下方式配置:用file前綴,指定配置文件的絕對路徑。貌似這種方式不是很友好。 如:locations = { "file:D:\\workspace\\webproxy\\src\\main\\resources\\" + "applicationContext.xml" }
完整代碼以下:
package com.shop.web.test; import java.util.Date; import java.util.List; import java.util.UUID; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import com.alibaba.fastjson.JSON; import com.shop.web.entity.ShopUser; import com.shop.web.service.ShopUserService; import com.shop.web.util.DateUtil; /** * service、dao層的測試類 * @author ces * */ @RunWith(SpringJUnit4ClassRunner.class)//表示繼承了SpringJUnit4ClassRunner類 @ContextConfiguration(locations = {"classpath:applicationContext-mybatis.xml","classpath:applicationContext-service.xml","classpath:applicationContext-transaction.xml"}) public class ShopControllerTest { private static Logger logger = Logger.getLogger(ShopControllerTest.class); @Resource private ShopUserService shopUserService; @Transactional @Test public void getShopUserById(){ ShopUser shopUser = new ShopUser(); shopUser.setUserid(6); shopUser.setUsername("zhangsan"); shopUser.setPassword("333"); shopUser.setCreateTime(Long.parseLong(DateUtil.getString(new Date(), DateUtil.YMDHMS))); try { shopUserService.insertSelective(shopUser); int i = 2/0; } catch (Exception e) { e.printStackTrace(); logger.info("ShopControllerTest" + e); } logger.info(JSON.toJSONString("*********")); } }