From: Cruz, E. J. <e....@ra...> - 2003-12-11 20:37:43
|
Hi, I have an interface described below. interface AccountBusiness { public UserTo authenticateUser(AccountTo account); } interface AccountDao { public AccountBusiness getAccount(account); } I have a method that uses this interface below: public UserTo authenticateUser(AccountTo account) { AccountBusiness accountBusiness = null; accountBusiness = getAccount(account); /** Some more stuff to perform authentication */ } private AccountBusiness getAccount(accounTo account) { AccountBusiness accountBusiness = null; try { defaultDaoManager.startTransaction(); accountBusiness = accountDao.getAccount(account); <------------ This line produces the error below. defaultDaoManager.commitTransaction(); } catch (DaoException de) { try { defaultDaoManager.rollbackTransaction(); } catch (DaoException de2) { log.error(de.getMessage(), de); throw new ResourceException(de); } return accountBusiness; } I am trying to test this method using Mock Objects. I have the following code to test this: public class AccountAppServiceTest extends TestCase { private AccountAppService accountAppService = null; private AccountTo account = null; private DaoManager defaultDaoManager = null; private Mock accountDao = null; protected void setUp() throws Exception { super.setUp(); defaultDaoManager = new DaoManagerMock(); accountDao = new Mock(AccountDAO.class); accountAppService = new AccountAppService (defaultDaoManager, (AccountDAO)accountDao.proxy()); account = new AccountTo(); account.setUsername("ecruz"); account.setPassword("testing"); account.setStatus("Active"); account.setBadPasswordCounter(2); account.setDatePasswordChanged(new Date()); account.setFirstLogin(true); } public void testAuthenticateUser() { //Setup Mock returnMock = new Mock(AccountBusiness.class); UserTo expectedUser = new UserTo(account.getUsername()); List expectedRoles = new ArrayList(); expectedRoles.add("test"); expectedRoles.add("test1"); AccountBusiness returnVal = (AccountBusiness)returnMock.proxy(); /** Here I tell it to expect the call to the accountDao getAccount method and to return the AccountBusiness Mock proxy but I receive the error below.*/ accountDao.expectAndReturn("getAccount", C.eq(account), (AccountBusiness)returnMock.proxy()); returnMock.expectAndReturn("authenticateUser", C.eq(account), expectedUser); accountDao.expectAndReturn("getAccountRoles", C.eq(account), expectedRoles); UserTo actualUser = null; //Run try { actualUser = accountAppService.authenticateUser(account); } catch (Exception e) { e.printStackTrace(); this.fail("Unexpected Exception reported " + e.toString()); } //Validate this.assertEquals("Unexpected UserName: ", expectedUser.getUserName(), actualUser.getUserName()); this.assertEquals("Unexpected Roles: ", expectedRoles, actualUser.getRoles()); accountDao.verify(); returnMock.verify(); } protected void tearDown() throws Exception { // Set everything to null } } I receive this error when the accountDao.getAccount(account) method is called above in the private getAccount method of the AccountAppService. I use the same type of return value in the expect in another test case and I do not get any errors. Any Suggestions on what this may be? [junit] ------------- Standard Error ----------------- [junit] java.lang.ClassCastException [junit] at $Proxy0.getAccount(Unknown Source) [junit] at gov.wos.domain.account.AccountAppService.getAccount(Unknown Source) [junit] at gov.wos.domain.account.AccountAppService.authenticateUser(Unknown Source) [junit] at gov.wos.domain.account.AccountAppServiceTest.testAuthenticateUser(Unknown Source) [junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39 ) [junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:25) [junit] at java.lang.reflect.Method.invoke(Method.java:324) [junit] at junit.framework.TestCase.runTest(TestCase.java:154) [junit] at junit.framework.TestCase.runBare(TestCase.java:127) [junit] at junit.framework.TestResult$1.protect(TestResult.java:106) [junit] at junit.framework.TestResult.runProtected(TestResult.java:124) [junit] at junit.framework.TestResult.run(TestResult.java:109) [junit] at junit.framework.TestCase.run(TestCase.java:118) [junit] at junit.framework.TestSuite.runTest(TestSuite.java:208) [junit] at junit.framework.TestSuite.run(TestSuite.java:203) [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRu nner.java:325) [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestR unner.java:524) [junit] ------------- ---------------- --------------- [junit] Testcase: testAuthenticateUser(gov.wos.domain.account.AccountAppServiceTest): FAILED [junit] Unexpected Exception reported java.lang.ClassCastException [junit] junit.framework.AssertionFailedError: Unexpected Exception reported java.lang.ClassCastException [junit] at gov.wos.domain.account.AccountAppServiceTest.testAuthenticateUser(Unknown Source) [junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39 ) [junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:25) Jamie Cruz Software Engineering e....@ra... (301)688-1430 |