`
langgufu
  • 浏览: 2288881 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用JUnit4测试Spring(转)

阅读更多

Spring环境下的JUnit4测试

1,下载所需jar包:

spring-test-3.2.0.RELEASE.jar

junit-4.11.jar

commons-dbcp-1.4.jar

 

2,配置Spring数据源:

spring-dao-test.xml

因为测试用例不是运行在Server环境下,不方便通过JNDI取得数据源,所以只能在Spring中自行配置,暂用DBCP(实际开发中建议不用DBCP,有BUG):

[html] view plaincopy
  1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  2.     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
  3.     <property name="url" value="jdbc:oracle:thin:@192.168.1.2:1523:test" />  
  4.     <property name="username" value="test" />  
  5.     <property name="password" value="test" />      
  6. </bean>  

 

3,测试DAO

SpringTest.java

[java] view plaincopy
  1. import static org.junit.Assert.*;  
  2.   
  3. import org.junit.Before;  
  4. import org.junit.Ignore;  
  5. import org.junit.Test;  
  6. import org.junit.runner.RunWith;  
  7. import javax.annotation.Resource;  
  8. import org.springframework.test.context.ContextConfiguration;  
  9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  10. import org.springframework.test.context.transaction.TransactionConfiguration;  
  11. import org.springframework.transaction.annotation.Transactional;  
  12.   
  13.   
  14. @Transactional  
  15. @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)  
  16. @RunWith(SpringJUnit4ClassRunner.class)  
  17. @ContextConfiguration(locations={"classpath:spring-dao-test.xml","classpath:spring-service-test.xml"})  
  18. public class SpringTest  
  19. {  
  20.     @Resource(name="testDao")  
  21.     private TestDao testDao;  
  22.   
  23.     @Before  
  24.     public void setUp() throws Exception  
  25.     {  
  26.     }  
  27.   
  28.     @Test  
  29.     public void testMyDao()  
  30.     {  
  31.         try  
  32.         {  
  33.             testDao.doSomething();  
  34.         }  
  35.         catch (Exception e)  
  36.         {  
  37.             fail("Test failed!");  
  38.         }  
  39.     }  
  40.   
  41.     @Ignore  
  42.     public void testOtherSpringObject()  
  43.     {  
  44.         fail("Not yet implemented");  
  45.     }  
  46. }  

 

4,测试Spring的Controller

(1)测试用例代码CreateProductControllerTest.java

[java] view plaincopy
  1. import static org.junit.Assert.*;  
  2. import org.junit.Before;  
  3. import org.junit.Test;  
  4. import org.junit.runner.RunWith;  
  5. import javax.annotation.Resource;  
  6. import org.springframework.http.HttpMethod;  
  7. import org.springframework.mock.web.MockHttpServletRequest;  
  8. import org.springframework.mock.web.MockHttpServletResponse;  
  9. import org.springframework.test.context.ContextConfiguration;  
  10. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  11. import org.springframework.test.context.transaction.TransactionConfiguration;  
  12. import org.springframework.transaction.annotation.Transactional;  
  13. import org.springframework.web.servlet.ModelAndView;  
  14.   
  15.   
  16. @Transactional  
  17. @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)  
  18. @RunWith(SpringJUnit4ClassRunner.class)  
  19. @ContextConfiguration(locations={"classpath:spring-servlet.xml""classpath:spring-dao-test.xml""classpath:spring-service-test.xml"})  
  20. public class CreateProductControllerTest  
  21. {  
  22.     @Resource(name="/createProduct.htm")  
  23.     CreateProductController createProductController;  
  24.       
  25.     private MockHttpServletRequest request;  
  26.   private MockHttpServletResponse response;  
  27.   
  28.     @Before  
  29.     public void before()  
  30.     {  
  31.         request = new MockHttpServletRequest();  
  32.         response = new MockHttpServletResponse();  
  33.         request.setCharacterEncoding("UTF-8");  
  34.     }  
  35.   
  36.     @Test  
  37.     public void testToSearchPage()  
  38.     {  
  39.         //request.setRequestURI("createProduct.htm");  
  40.         //request.setMethod(HttpMethod.POST.name());  
  41.           
  42.         ModelAndView mv = null;  
  43.         try  
  44.         {  
  45.             mv = createProductController.toSearchPage(request, response);  
  46.         }  
  47.         catch (Exception e)  
  48.         {  
  49.             e.printStackTrace();  
  50.             fail("testToSearchPage failed!");  
  51.         }  
  52.   
  53.         assertNotNull(mv);  
  54.         assertEquals(response.getStatus(), 200);  
  55.     }  
  56. }  

 

5,注意被测试对象在Spring中不能配置AOP切面代理,否则注入到TestCase时,会产生类型不匹配的异常。因为被代理后的类型发生了变化,注入到TestCase中时,与原始的类型有区别

另外运行TestCase时,可能需要加上两个jvm参数:

-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics