UserMgmtServerEao { public void persist(UserMgmtServer usrMgmtServer) { encryptPassword(usrMgmtServer); UserMgmtServerEntity userMgmtServerEntity = userMgmtServerDao.findById(usrMgmtServer.getName()); if (userMgmtServerEntity != null) { ValidationError validationError = new ValidationError("NAME.DUPLICATION", "Same name already exists"); ValidationErrors errors = new ValidationErrors(); errors.addError("Name", validationError); throw new ValidationException(errors.getErrors()); } userMgmtServerEntity = new UserMgmtServerEntity(); userMgmtServerEntity.copyFrom(usrMgmtServer); userMgmtServerDao.insert(userMgmtServerEntity); }  void persist(UserMgmtServer usrMgmtServer); @Transactional(propagation = Propagation.SUPPORTS) UserMgmtServer findByName(String name, boolean safely); void delete(String name); void checkServerChanged(UserMgmtServer userMgtServer); void modify(UserMgmtServer usrMgmtServer);  }
@Test(groups = {"TestUserMgmtServerEao"}) public void testPersist() { userMgmtServerDao = new MockUp<IBaseDAO<UserMgmtServerEntity>>() { @Mock(invocations = 1) public UserMgmtServerEntity findById(Serializable id) { return null; } @Mock(invocations = 1) public void insert(UserMgmtServerEntity userMgmtServerEntity) { assertUserMgmtServerEntity(userMgmtServerEntity); } }.getMockInstance(); TestUtils.setPrivateField(userMgmtServerEao, "userMgmtServerDao", userMgmtServerDao); UserMgmtServer userMgmtServer = prepareUserMgmtServer(true); userMgmtServerEao.persist(userMgmtServer); }
@Test(groups = {"TestUserMgmtServerEao"}, expectedExceptions = ValidationException.class) public void testPersistFailed() { userMgmtServerDao = new MockUp<IBaseDAO<UserMgmtServerEntity>>() { @Mock(invocations = 1) public UserMgmtServerEntity findById(Serializable id) { UserMgmtServerEntity userMgmtServerEntity = new UserMgmtServerEntity(); userMgmtServerEntity.setName(name); return userMgmtServerEntity; } @Mock(invocations = 0) public void insert(UserMgmtServerEntity userMgmtServerEntity) { } }.getMockInstance(); TestUtils.setPrivateField(userMgmtServerEao, "userMgmtServerDao", userMgmtServerDao); UserMgmtServer userMgmtServer = new UserMgmtServer(); userMgmtServerEao.persist(userMgmtServer); }