`

使用jUnit spring来联合测试

阅读更多

 

原文引自:http://blog.csdn.net/yriio/article/details/6678948

 

spring配置文件:

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="  
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd  
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  10.   
  11.     <bean id="testService" class="cn.lik.airTycoon.JunitTestServiceImpl">  
  12.     </bean>  
  13.       
  14. </beans>  



 

service:

 

  1. package cn.lik.airTycoon;  
  2.   
  3. public class JunitTestServiceImpl {  
  4.   
  5.     public JunitTestServiceImpl() {  
  6.         System.out.println("....init....");  
  7.     }  
  8.   
  9.     public boolean isReal(boolean is) {  
  10.         return !is;  
  11.     }  
  12. }  


测试用例1, 用的注释注入,这个注释注入是可以被继承的,也就是说写个抽象 类,配置好配置文件就可以了,其他测试用例继承这个就好

 

 

  1. package test.airTycoon;  
  2.   
  3. import junit.framework.Assert;  
  4.   
  5. import org.junit.Test;  
  6. import org.junit.runner.RunWith;  
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.test.context.ContextConfiguration;  
  9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  10.   
  11. import cn.lik.airTycoon.JunitTestServiceImpl;  
  12.   
  13. @RunWith(SpringJUnit4ClassRunner.class)  
  14. @ContextConfiguration(locations = { "classpath:applicationContext.xml" })  
  15. public class AirtyCoonTest {  
  16.   
  17.     @Autowired  
  18.     private JunitTestServiceImpl testService;  
  19.   
  20.     @Test  
  21.     public void test() {  
  22.         Assert.assertTrue(testService.isReal(false));  
  23.     }  
  24.   
  25.     public JunitTestServiceImpl getTestService() {  
  26.         return testService;  
  27.     }  
  28.   
  29.     public void setTestService(JunitTestServiceImpl testService) {  
  30.         this.testService = testService;  
  31.     }  
  32. }  


 

用例2 这个就没有写spring 的配置文件以及运行类,直接继承过来

 

  1. package test.airTycoon;  
  2.   
  3. import org.junit.Assert;  
  4. import org.junit.Test;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6.   
  7. import cn.lik.airTycoon.JunitTestServiceImpl;  
  8.   
  9. public class AirtyCoonTest2 extends AirtyCoonTest {  
  10.     @Autowired  
  11.     private JunitTestServiceImpl testService;  
  12.   
  13.     @Test  
  14.     public void test() {  
  15.         Assert.assertTrue(testService.isReal(false));  
  16.     }  
  17.   
  18. }  


联合测试: 需要测试的类追加在后面就可以。

 

 

  1. package test.airTycoon;  
  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({ // test List  
  9. AirtyCoonTest.class//  
  10.         AirtyCoonTest2.class //  
  11. })  
  12. public class AllTests {  
  13.   
  14. }  


相当的方便阿,而且eclipse原生就是支持jUnit了,加上spring还有对junit的支持类,可以做到测试用例结束以后自动回滚数据库数据,完全不用担心由于测试而形成的垃圾数据

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics