title: EasyMock Exception Handling
body: <p>I am creating some junit test cases using EasyMock.  So far it makes sense for classes I am expecting to return POJOs, but how should I handle dealing with a DAO object that itself could throw an exception.  My test case is to check for an expected exception thrown when the DAO encounters an issue.  Using EasyMock I try and mock the DAO object (testing from the <code>foo</code> class), what is the correct way to handle the lower level DAO exception.</p>
  
  <p>An example of the classes/simple calls is below:  (Assume all getters/setters/constructors are valid and present)</p>
  
  <pre><code>public class foo{
      private daoClass dao = daoClass.getInstance();
      public String getValueFromDB(String key) throws DBException{
          return dao.lookup(key);
      }
  }
  
  public class daoClass{ //singleton DAO 
      public daoClass getInstance(){
         //singleton access here
      }
      public String lookup(String key) throws DBException{
          try{
              //DB LOGIC
          }
          catch(Exception e){
              throw new DBException(e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>When I try and test the <code>foo</code> class, I want to be able to test for this DBException.  How do I handle this, should I be surronding the DAO call in a try/catch (in the test), or add a throws to the test?  I know <code>expected=DBException</code> will pass the test if that is thrown, but how syntactically should you handle any number of inner exceptions?</p>
  
  <p>Test code Example:</p>
  
  <pre><code>@Test(expected=DBException.class)
  public void testFooError(){
      String key = "test";
      String value = "expected";
      daoClass daoMock = createMock(daoClass.class);
      try{
          expect(daoMock.lookup(key)).andReturn(value);
      } catch (DBException e){
          // ???
      }
  }
  </code></pre>
  
  <p>What is the correct way to handle when the <code>expect</code> could potentially throw errors?  Should the test method <code>throw</code> the exception, or should a <code>try/catch</code> be used?  Is it still correct to use the <code>expected=EXCEPTION</code> tag on the test?</p>