0

I am in the progress of writing some code that prints on an org.eclipse.swt.printing.Printer. So first step is to see how I can test this, but it seems the architecture doesn't allow me to define my own printer, since neither PrinterData nor Printer are interfaces as they should, or at least an abstract class.

I could probably just mock the GC object that gets printed on, but this would be really hard since I use a third-party library to do the actual printing. (And to be honest, GC objects are not the best for unit tests.)

Or I could just test the models before they get printed, however there is still a big step missing in the test.

Is there a way to mock a Printer so I can test my code fully?

Baz
  • 36,440
  • 11
  • 68
  • 94
Stefan S.
  • 3,950
  • 5
  • 25
  • 77

1 Answers1

1

I usually mock only types that I own (Should you only mock types you own?).

Now that it is even technically near-impossible to mock final types like Printer, I would write a printer abstraction. This class would only expose the printer API that the application actually relies on. Internally the abstraction could use an instance of Printer to delegate to.

This leaves you with a class or interface that is safe to mock and only the delegation code would be untested.

Community
  • 1
  • 1
RĂ¼diger Herrmann
  • 20,512
  • 11
  • 62
  • 79