Hibernate第三步:
开发测试用例AddressDAOTest.java测试AddressDAO.java
通常开发完DAO层的代码 都需要辨析JUnit测试用例来进行数据层测试 下面我们就来编写一个测试用例类来测试AddressDAO.java 包括
[1]编写单元测试类AddressDAOTest.java
[2]编写测试套件类AllTest.java
[1]编写单元测试类AddressDAOTest.java
通常测试用例类都以“*Test”形式命名 该类必须继承TestCase 并包含一个addressDAO对象 该对象在setUp()中通过FileSystemXmlApplicationContext类来读取Bean的配置文件 然后使用getBean()取得。
- package com.demo.hibernate.test;
- import java.util.Iterator;
- import java.util.List;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- import com.demo.hibernate.beans.Address;
- import com.demo.hibernate.dao.IAddressDAO;
- import com.demo.hibernate.dao.AddressDAO;
- import junit.framework.TestCase;
- public class AddressDAOTest extends TestCase {
- public IAddressDAO addressDAO;
- public AddressDAOTest(String name) {
- super(name);
- }
- protected void setUp() throws Exception {
- super.setUp();
- addressDAO = new AddressDAO();
- ApplicationContext ctx = new FileSystemXmlApplicationContext(
- "WebRoot/WEB-INF/applicationContext.xml");
- addressDAO = (IAddressDAO) ctx.getBean("addressDAO");
- }
- protected void tearDown() throws Exception {
- super.tearDown();
- }
- public void testFindAllByUsername() throws Exception {
- List list = addressDAO.findAllByUsername("admin");
- assertTrue(list.size() == 0);
- Iterator it = list.iterator();
- while (it.hasNext()) {
- Address address = (Address) it.next();
- assertNotNull(address);
- }
- }
- public void testFindById() throws Exception {
- Address address = addressDAO.findById("1");
- assertNotNull(address);
- }
- public void testI***ist() throws Exception {
- boolean b = addressDAO.i***ist("admin", "lzb");
- assertTrue(b);
- }
- public void testInsert() throws Exception {
- Address address = new Address();
- address.setUsername("admin");
- address.setName("andy");
- address.set***("2");
- address.setMobile("13888886666");
- address.setEmail("andy@163.com");
- address.setQq("12345678");
- address.setCompany("Intel");
- address.setAddress("上海市");
- address.setPostcode("200089");
- addressDAO.insert(address);
- address = addressDAO.findById("2");
- assertNotNull(address);
- }
- public void testUpdate() throws Exception {
- Address address = new Address();
- address.setId(new Integer(2));
- address.setUsername("admin");
- address.setName("andy");
- address.set***("2");
- address.setMobile("13888886666");
- address.setEmail("andy@163.com");
- address.setQq("12345678");
- address.setCompany("Microsoft");
- address.setAddress("上海市");
- address.setPostcode("200089");
- addressDAO.update(address);
- Address address2 = addressDAO.findById("2");
- assertTrue(address2.getCompany().equals("Microsoft"));
- }
- public void testDelete() throws Exception {
- addressDAO.delete("2");
- Address address = addressDAO.findById("2");
- assertNull(address);
- }
- }
[2]编写测试套件类AllTest.java
编写一个测试套件类AllTest.java 用来启动addressDAOTest.java的测试代码 该类必须继承TestCase 并编写一个测试入口函数suite() 在该函数中创建一个TestSuite类型的变量 并使用addTest()函数增加addressDAOTest.class的测试类 然后返回该套件变量suite。
然后编写一个主函数main() 使用TestRunner来运行suite()函数
- package com.demo.hibernate.test;
- import junit.framework.Test;
- import junit.framework.TestCase;
- import junit.framework.TestSuite;
- public class AllTest extends TestCase {
-
- public static void main(String[] args) {
- junit.textui.TestRunner.run(suite());
- }
-
- public static Test suite() {
- TestSuite suite = new TestSuite("Test for test");
- suite.addTest(new TestSuite(AddressDAOTest.class));
- return suite;
- }
- }
用鼠标右键单击测试套件类AllTest.java 在弹出的快捷菜单中选择运行-JUnit命令 即开始测试以上六个函数。
如果现实的绿色的进度条 说明6个任务都测试成功了 这样就可以放心食用AddressDAO.java中的函数了
阅读(1579) | 评论(0) | 转发(0) |