Unit tests for printing in Java Swing

I have code that uses the Swing / AWT print function that I want to use unit test. It uses the native system's print dialog box, but is there a way to get the JVM to intercept this and replace it with a layout during unit testing so that I can get a copy of the image to be printed?

My code for printing is simple and looks something like this:

Printable printable = getPrintable(); PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(printable); if (printJob.printDialog()) { try { printJob.print(); } catch (PrinterException exception) { ... } } 

I use FEST for my other graphics tests, but it doesn't seem to support printing tests. Is it possible to do this, or will I need to write test documentation that includes QA transfers to check their printer?

+6
source share
1 answer

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

You can create standard printing and generate images for each page to be displayed.

+2
source

Source: https://habr.com/ru/post/917092/


All Articles