snowphy 2011-02-22
使用Maven+Junit做单元测试竟是如此畅快。只需要几步就完成了单元测试。
1、编写单元测试
在src/test/java下编写单元测试,可以编写N个测试。下面是其中一个。
public class OrderTest extends TestCase { private ApplicationContext ac; private MyorderDao dao; public MyorderDao getDao() { if (dao == null) { dao = (MyorderDao) ac.getBean("myorderDao"); } return dao; } /** * 测试分页查询 */ public void testQueryPaging() { Myorder example = new Myorder(); ... ... List<Myorder> orderList = getDao().selectByExamplePaging(example); for (Myorder myorder : orderList) { ... ... } } protected void setUp() throws Exception { ac = new ClassPathXmlApplicationContext("applicationContext-dao.xml"); super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); ac = null; } }
2、Maven运行单元测试
cd到项目目录运行:mvntest
执行maven命令后做所有的类和方法的测试并打印测试结果