Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit tests for printing in Java Swing

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?

like image 443
Jordan Bentley Avatar asked May 31 '12 12:05

Jordan Bentley


1 Answers

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

like image 85
ControlAltDel Avatar answered Oct 25 '22 16:10

ControlAltDel