Unit tests for printing in Java Swing

java, swing, testing

Solution

Check out my print library: http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/print/

You can create a StandardPrint and generate images for each page that will be rendered

Problem

I have some code that uses the Swing/AWT printing functionality that I want to unit test. It uses the systems native print dialog, but is there any way to get the JVM to intercept that and replace it with a mock during unit testing so that I can get a copy of the image that would be printed? My code for printing is straightforward and looks roughly like this: ``` Printable printable = getPrintable(); PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(printable); if (printJob.printDialog()) { try { printJob.print(); } catch (PrinterException exception) { ... } } ``` I've been using FEST for my other GUI tests but it does not seem to have any support for printing tests. Is this even possible to do, or will I need to write up testing documentation that involves telling QA to go check their printer?

Original source