您的位置:首页 > 编程语言 > Java开发

边做边学小型封装--利用主流框架进行Dao层通用化,Spring3.0+Hibernate3.3.2通用Dao层整合(五)

2010-02-26 11:57 507 查看
当所有工作准备完成后接下来就是测试了,测试的话就非常简单了,在Eclipse中加入Junit测试包,或者可以到Junit的官方网站下载Junit4.8.1.Jar包放入lib文件夹中,创建一个JunitTestCase或者创建一个类,输入以下代码:

package org.lxh.test;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.lxh.bean.User;
import org.lxh.service.interfaces.UserServiceIf;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserxTest {

private static UserServiceIf userService;

@Before
public  void setUpBeforeClass() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
userService =(UserServiceIf)applicationContext.getBean("userService");
//	userService =(UserServiceIf)applicationContext.getBean("userService");
}

@After
public  void tearDownAfterClass() throws Exception {
}

@Test
public void testClearAllUser() {
try {
//	userService.clearAllUser(userService.listUserById(User user,1));
} catch (Exception e) {
e.printStackTrace();
fail("Not yet implemented");
}

}

@Test
public void testClearUserById() {
try {
userService.clearUserById(1);
} catch (Exception e) {
e.printStackTrace();
fail("Not yet implemented");
}

}

@Test
public void testListAllUser() {
try {
List<User> list =userService.listAllUser();
for (User user : list) {
System.out.println(user.getUsername()+":"+user.getPassword());
}
} catch (Exception e) {
e.printStackTrace();
fail("Not yet implemented");
}
}

@Test
public void testListUserById() {
try {
User user =userService.listUserById(2);
System.out.println(user.getUsername()+":"+user.getPassword());
} catch (Exception e) {
e.printStackTrace();
fail("Not yet implemented");
}
}

@Test
public void testModifyUser() {
try {
//		User user =	userService.listUserById(1);
//		user.setUsername("fuck");
//			userService.modifyUser(user);
} catch (Exception e) {
e.printStackTrace();
fail("Not yet implemented");
}

}

@Test
public void testNewUser() {
try {
User user = new User();
user.setUsername("edward");
user.setPassword("lxh");
userService.newUser(user);
} catch (Exception e) {
e.printStackTrace();
fail("Not yet implemented");
}

}

}


输入上述代码就可以完整整个通用测试了.

总结一下:因为这个封装只是单单针对的比较简单的增删查改的功能,只是把我们每个经常使用的CRUD功能完全集成一个Dao里面,不用我们对每个表进行独立的Dao创建,只是在Service层中详细把每个表分开进行操作,维护性比较高,只是单独维护Dao就可以了,需要增加功能或者删除功能完全统一完成可以直接应用到各各Service中,如果利用工厂模式的话维护性更加方便.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐