1

In DAO class

catch (Exception e) { throw new DaoException(e); } 

In Junit Class

@Test public void testClass() throws DaoException { try { doThrow(Exception.class).when(testDAO).getTestData(); testDAO.getTestData(); } catch (Exception e) { e.printStackTrace(); } } 

Unable to cover DaoException.Could you please help me to cover DAOException in JUNIT.

2
  • can't you send bad / wrong data to force the exception? Commented Aug 5, 2016 at 9:25
  • What is the code of method getTestData? Commented Aug 5, 2016 at 9:28

1 Answer 1

2

You should NOT enclose it in try catch block within your JUnit. Looks like you want to verify whether DaoException class being thrown.

Since you are already using JUnit 4, below is the syntax for it:

@Test(expected=DaoException.class) public void testClass() { doThrow(Exception.class).when(testDAO).getTestData(); testDAO.getTestData(); } 

The snippet above will pass the test if it throws DaoException.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.