Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are two parts to any test: getting it to happen and measuring that you got the correct result.</p> <h2>Fault Injection</h2> <p>The easiest answer is the one that's already been mentioned, which is to set <code>cacheFileName</code> to a file that will never exist. This is likely the most practical answer in this situation.</p> <p>However, to cause an arbitrary condition such as an <code>IOException</code>, what you really want is <a href="http://en.wikipedia.org/wiki/Fault_injection" rel="nofollow noreferrer">Fault Injection</a>. This forces faults in your code without forcing you to instrument your source code. Here are a few methods for doing this:</p> <ul> <li><strong>Mock objects</strong> You could use a factory method to create an overridden <code>ObjectOutputStream</code> or <code>FileOutputStream</code>. In test code the implementation would throw an <code>IOException</code> when you wanted to, and in production code would not modify the normal behavior.</li> <li><strong>Dependency Injection</strong> In order to get your Mock Object in the right place you could use a framework such as <a href="http://www.springsource.org/" rel="nofollow noreferrer">Spring</a> or <a href="http://www.seamframework.org/" rel="nofollow noreferrer">Seam</a> to "inject" the appropriate object into the class that's doing the work. You can see that these frameworks even have a priority for objects that will be injected, so that during unit testing you can override the production objects with test objects.</li> <li><strong><a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" rel="nofollow noreferrer">Aspect Oriented Programming</a></strong> Instead of changing the structure of your code at all, you can use AOP to inject the fault in the right place. For instance, using <a href="http://www.eclipse.org/aspectj/" rel="nofollow noreferrer">AspectJ</a> you could define a <a href="http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html#pointcuts" rel="nofollow noreferrer">Pointcut</a> where you wanted the exception to be thrown from, and have the <a href="http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html#advice" rel="nofollow noreferrer">Advice</a> throw the desired exception.</li> </ul> <p>There are other answers to fault injection on Java; for instance a product called <a href="http://www.ocsystems.com/tech_aprobe.html" rel="nofollow noreferrer">AProbe</a> pioneered what could be called AOP in C long ago, and they also have a Java product.</p> <h2>Validation</h2> <p>Getting the exception thrown is a good start, but you also have to validate that you got the right result. Assuming that the code sample you have there is correct, you want to validate that you logged that exception. Someone above mentioned using a Mock object for your logger, which is a viable option. You can also use AOP here to catch the call to the logger.</p> <p>I assume that the logger is <a href="http://logging.apache.org/log4j/1.2/" rel="nofollow noreferrer">log4j</a>; to solve a similar problem, I implemented my own log4j appender which captures log4j output: I specifically capture only <code>ERROR</code> and <code>FATAL</code>, which are likely to be the interesting log messages in such a case. The appender is referenced in <code>log4j.xml</code> and is activated during the test run to capture error log output. This is essentially a mock object, but I didn't have to restructure all my code that got a log4j <code>Logger</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload