@Test public void testSaveAccountForReal() { try { Account account = new Account("54321", "Joe Soap"); // Now call service method bankService.save(account); Account newAccount = bankService.getAccount(account.getAccountNumber()); Assert.assertEquals(account.getAccountNumber(), newAccount.getAccountNumber()); } catch (BankServiceException e) { LOG.error(e.getMessage(), e); Assert.fail("Unexpected exception thrown during test: " + e.getMessage()); } }
@Test(expected = BankServiceException.class) public void testSaveAccountFailNoConnection() throws BankServiceException, TestException { // Setup mocking BankDAO dao = createMock(BankDAO.class); try { Account account = new Account("12345", "Joe Soap"); dao.save(account); expectLastCall() .andThrow( new CannotGetJdbcConnectionException( "Can't get connection.", new SQLException("Dummy SQL Exception."))); replay(dao); // Inject the DAO into the service object using reflection injectField(bankService, "bankDAO", dao); // Now call service method bankService.save(account); } catch (BankDataException e) { LOG.error(e); Assert.fail("Unexpected exception thrown during test: " + e.getMessage()); } finally { // finally, restore the real DAO so other tests // expecting a real database can use it. injectField(bankService, "bankDAO", realBankDAO); } }