Exceptions, jUnit integration testing, and conventions
ventmonkey Feb 21, 2011 4:02 PMHi all. This is more of a discussion than a technical question. Lets say I have the following (simplified) AccountService bean (which handles stuff for an Account)
@Name("accountService") @Stateless public class AccountService implements Account, Serializable { public AccountService(com.mydomain.persistence.Account account) { this.account = account; } public com.mydomain.Persistence.Account register() { if (this.account.getEmailAddress() == null) { throw new IllegalArgumentException("account.emailAddress cannot be null"); } com.mydomain.Persistence.Account registeredAccount = persistAccount(this.account); return registeredAccount; } }
and an AccountServiceTest of
@Test(expected = IllegalArgumentException.class) public void testRegisterAccountEmptyEmailAddress() { Lifecycle.beginCall(); Account registerAccount = new Account(); registerAccount.setUserName("failAccount1"); registerAccount.setPasswordPlainText("failAccount1"); //registerAccount.setEmailAddress("failAccount1@mydomain.com"); AccountService as = new AccountService(registerAccount); as.register(); Lifecycle.endCall(); }
I like having my java code to be able to throw Exceptions if need be, and not have all of the FacesMessages stuff in it. I think it makes testing very clear.
So here is the discussion part. What is the best way to handle building the page bean? Do I have my registration on the page be something like
#{accountServiceUI.register(account)}
that points to a page like
@Name("accountServiceUI") public class AccountServiceUi { public boolean register(Account account) { if (account.getEmailAddress == null) { FacesMessages.instance().addFromResourceBundleOrDefault(...); } AccountSerive as = new AccountService(account); as.register } }
or add FacesMessage to my AccountService bean, and have AccountServiceUI just catch any exceptions so the end user only sees the FacesMessages? Or would the standard just be to only report errors in AccountService to FacesMessages and not throw Exceptions?
I realize any of those ways would work, but I would like to figure out the best practice.
Thanks,
Mason