Search code examples
javajunitcode-coveragedaosqlexception

How do I provoke a SQLExcetpion in DAO-Layer with JUnit


I have a DAO-Layer which I want to test with JUnit. The test coverage is about 85%, so nearly every line of code is covered except the catch clauses for SQLException. I have no idea how I can provoke a SQLException for the snippet below:

Code coverage report
The only conceivable solution for me is to reset the value of the final variable SELECT_ALL_ATTR to something like: SELLLLLECT * FROM ATTRIBUTES; by using java reflection. I know that I can easily test exceptions by using the Mock framework. But I don't want to introduce Mock tests just for exception testing since everything else is tested with JUnit.

Is there a way other than java reflection?


Solution

  • It's not easy to simulate a sql exception in your case because all dependences are initiated by the dao itself. Maybe you can pass a fake Connection which throws a SqlException in some method?