测试DAO
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");
}
}
测试Spring的Controller
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);
}
}
注意被测试对象在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