使用JUnit4测试Spring
测试DAO
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 }
测试Spring的Controller
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 }
注意被测试对象在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