在Maven+Hibernate4注解0配置示例中创建用户操作数据库的模块,现在创建用户管理服务模块user-service。
user-service模块是要依赖user-dao模块的,怎么用maven来管理依赖呢?
1.在user-dao模块下,pom.xml中右键,选择maven install命令,将user-dao发布到本地仓库中;
2.在user-service模块下,pom.xml中添加对user-dao模块的依赖。Maven会将user-dao中依赖的Jar一同拷贝过来。
user-service模块包括:
1个用户操作服务接口类IUserService;
1个用户操作服务实现类UserService;
1个单元测试类TestUserSerivice。
IUserService中包含3个方法,分别为添加用户,根据用户名查找某个用户,以及列出所有的用户。
单元测试使用easymock来mock对象的预期行为。
工程结构图:
代码:
pom.xml:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.purple_river.itat.maven.demo.service</groupId> 6 <artifactId>user-service</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 10 <name>user-service</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <version>4.10</version> 22 <scope>test</scope> 23 </dependency> 24 <dependency> 25 <groupId>com.purple_river.itat.maven.demo.user.dao</groupId> 26 <artifactId>user-dao</artifactId> 27 <version>0.0.1-SNAPSHOT</version> 28 </dependency> 29 <dependency> 30 <groupId>org.easymock</groupId> 31 <artifactId>easymock</artifactId> 32 <version>3.2</version> 33 </dependency> 34 35 </dependencies> 36 </project>
IUserService:
1 /* 2 * @packageName:com.purple_river.itat.maven.demo.service 3 * @fileName:IUserService.java 4 * @description:用户管理接口类 5 * @author:luckystar2010 6 * @date:2013-11-29 7 */ 8 package com.purple_river.itat.maven.demo.service; 9 10 import java.util.List; 11 12 import com.purple_river.itat.maven.demo.bean.user.User; 13 14 public interface IUserService { 15 16 public void addUser(User user); 17 18 public User queryUserByUserName(String username); 19 20 public List<User> queryAll(); 21 }
UserService:
1 /* 2 * @packageName:com.purple_river.itat.maven.demo.service 3 * @fileName:UserService.java 4 * @description:用户管理服务类 5 * @author:luckystar2010 6 * @date:2013-11-29 7 */ 8 package com.purple_river.itat.maven.demo.service; 9 10 import java.util.List; 11 12 import com.purple_river.itat.maven.demo.bean.user.User; 13 import com.purple_river.itat.maven.demo.user.dao.IUserDao; 14 15 public class UserService implements IUserService { 16 private IUserDao userDao; 17 18 public UserService(IUserDao userDao) { 19 this.userDao = userDao; 20 } 21 22 /* (non-Javadoc) 23 * @see com.purple_river.itat.maven.demo.service.IUserService#addUser(com.purple_river.itat.maven.demo.bean.user.User) 24 */ 25 public void addUser(User user) { 26 this.userDao.addUser(user); 27 } 28 29 /* (non-Javadoc) 30 * @see com.purple_river.itat.maven.demo.service.IUserService#queryUserByUserName(java.lang.String) 31 */ 32 public User queryUserByUserName(String username) { 33 return this.userDao.getUser(username); 34 } 35 36 /* (non-Javadoc) 37 * @see com.purple_river.itat.maven.demo.service.IUserService#queryAll() 38 */ 39 public List<User> queryAll() { 40 return this.userDao.getAll(); 41 } 42 43 }
TestUserService:
1 /* 2 * @packageName:com.purple_river.itat.maven.demo.service 3 * @fileName:TestUserService.java 4 * @description:用户管理服务测试类 5 * @author:luckystar2010 6 * @date:2013-11-29 7 */ 8 package com.purple_river.itat.maven.demo.service; 9 10 import java.util.ArrayList; 11 import java.util.Calendar; 12 import java.util.List; 13 14 import junit.framework.Assert; 15 16 import org.easymock.EasyMock; 17 import org.jboss.logging.Logger; 18 import org.junit.After; 19 import org.junit.Before; 20 import org.junit.Test; 21 22 import com.purple_river.itat.maven.demo.bean.user.User; 23 import com.purple_river.itat.maven.demo.user.dao.IUserDao; 24 25 public class TestUserService { 26 public final static Logger logger = Logger.getLogger(TestUserService.class); 27 private IUserService userService; 28 private IUserDao userDao; 29 30 @Before 31 public void setUp() { 32 //this.userDao = EasyMock.createMock(IUserDao.class); 33 this.userDao = EasyMock.createMock(IUserDao.class); 34 logger.info("created mock:" + this.userDao); 35 this.userService = new UserService(this.userDao); 36 } 37 38 @Test 39 public void testAddUser() { 40 logger.info("Test [UserService.addUser]."); 41 42 Calendar c = Calendar.getInstance(); 43 c.set(2012, 1,1); 44 User user = new User("admin",1,c.getTime()); 45 // 当Mock的对象调用的是返回值为void方法时,可以用EasyMock.expectLastCall();来设置返回值是void 46 // 或者不用显示的设定它的返回值。 47 // 比如下面2行 48 // this.userDao.addUser(user); 49 // EasyMock.expectLastCall(); 50 // 可以不要。 51 this.userDao.addUser(user); 52 EasyMock.expectLastCall(); 53 this.userService.addUser(user); 54 } 55 56 @Test 57 public void testQueryByUesrName() { 58 logger.info("Test [UserService.queryByUserName]"); 59 60 // record 61 User user = new User(); 62 user.setUsername("admin"); 63 user.setAge(1); 64 EasyMock.expect(this.userDao.getUser("admin")).andReturn(user); 65 66 // replay 67 EasyMock.replay(this.userDao); 68 User u = this.userService.queryUserByUserName("admin"); 69 70 // verify 71 Assert.assertEquals(u.getUsername(), "admin"); 72 Assert.assertEquals(u.getAge(), 1); 73 74 EasyMock.verify(this.userDao); 75 } 76 77 @Test 78 public void testQueryAll() { 79 logger.info("Test [UserService.queryAll]."); 80 81 // record,设置方法的返回值 82 User user = new User(); 83 user.setUsername("admin"); 84 user.setAge(1); 85 List<User> userList = new ArrayList<User>(); 86 userList.add(user); 87 // 设定当UserService调用UserDao.getAll()方法时返回userList 88 EasyMock.expect(this.userDao.getAll()).andReturn(userList); 89 90 // replay,在调用方法时EasyMock将我们设置的值返回 91 EasyMock.replay(this.userDao); 92 List<User> list = this.userService.queryAll(); 93 94 // verify,验证方法返回的结果与我们预期的是否一致 95 Assert.assertEquals(list.size(), 1); 96 EasyMock.verify(this.userDao); 97 } 98 99 @After 100 public void tearDown() { 101 this.userDao = null; 102 this.userService = null; 103 } 104 }
下载地址:http://files.cnblogs.com/luckystar2010/user-service.rar