使用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):
import static org.junit.Assert.*; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import javax.annotation.Resource; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @Transactional @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:spring-dao-test.xml","classpath:spring-service-test.xml"}) public class SpringTest { @Resource(name="testDao") private TestDao testDao; @Before public void setUp() throws Exception { } @Test public void testMyDao() { try { testDao.doSomething(); } catch (Exception e) { fail("Test failed!"); } } @Ignore public void testOtherSpringObject() { fail("Not yet implemented"); } }
4,测试Spring的Controller
(1)测试用例代码CreateProductControllerTest.java
import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import javax.annotation.Resource; import org.springframework.http.HttpMethod; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.servlet.ModelAndView; @Transactional @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:spring-servlet.xml", "classpath:spring-dao-test.xml", "classpath:spring-service-test.xml"}) public class CreateProductControllerTest { @Resource(name="/createProduct.htm") CreateProductController createProductController; private MockHttpServletRequest request; private MockHttpServletResponse response; @Before public void before() { request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); request.setCharacterEncoding("UTF-8"); } @Test public void testToSearchPage() { //request.setRequestURI("createProduct.htm"); //request.setMethod(HttpMethod.POST.name()); ModelAndView mv = null; try { mv = createProductController.toSearchPage(request, response); } catch (Exception e) { e.printStackTrace(); fail("testToSearchPage failed!"); } assertNotNull(mv); assertEquals(response.getStatus(), 200); } }
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
相关推荐
shirleypaddy 2020-10-19
qingmumu 2020-10-19
lustdevil 2020-10-18
ganlulululu 2020-10-12
lustdevil 2020-08-03
lustdevil 2020-07-18
lustdevil 2020-06-25
lustdevil 2020-06-21
zhengzf0 2020-06-21
宿命java 2020-06-15
JackYin 2020-06-14
dongxurr 2020-06-07
snowphy 2020-06-06
zhengzf0 2020-05-28
81901836 2020-05-26
Julywhj 2020-05-26