UserMgmtServerEao { public void delete(String name) { UserMgmtServerEntity userMgmtServerEntity = userMgmtServerDao.findById(name); if (userMgmtServerEntity != null) { userMgmtServerDao.delete(userMgmtServerEntity); } else { ValidationError validationError = new ValidationError("NAME.NOT_FOUND", "given name not found."); ValidationErrors errors = new ValidationErrors(); errors.addError("Name", validationError); throw new ValidationException(errors.getErrors()); } }  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 testDelete() { userMgmtServerDao = new MockUp<IBaseDAO<UserMgmtServerEntity>>() { @Mock(invocations = 1) public UserMgmtServerEntity findById(Serializable id) { UserMgmtServerEntity userMgmtServerEntity = new UserMgmtServerEntity(); userMgmtServerEntity.setName(name); return userMgmtServerEntity; } @Mock(invocations = 1) public void delete(UserMgmtServerEntity userMgmtServerEntity) { assertEquals(userMgmtServerEntity.getName(), name); } }.getMockInstance(); TestUtils.setPrivateField(userMgmtServerEao, "userMgmtServerDao", userMgmtServerDao); userMgmtServerEao.delete(name); }
@Test(groups = {"TestUserMgmtServerEao"}, expectedExceptions = ValidationException.class) public void testDeleteFailed() { userMgmtServerDao = new MockUp<IBaseDAO<UserMgmtServerEntity>>() { @Mock(invocations = 1) public UserMgmtServerEntity findById(Serializable id) { return null; } @Mock(invocations = 0) public void delete(UserMgmtServerEntity userMgmtServerEntity) { } }.getMockInstance(); TestUtils.setPrivateField(userMgmtServerEao, "userMgmtServerDao", userMgmtServerDao); userMgmtServerEao.delete(name); }