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):
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.1.2:1523:test" />
<property name="username" value="test" />
<property name="password" value="test" />
</bean>
3,测试DAO
SpringTest.java
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