Mocking SWT Image and Display with PowerMockito
I’ve been working in SWT quite a bit during my career at ReadyTalk. In many cases, I’ll need to mock out two common SWT classes,
Display. Unfortunately, mocking out these classes (or static calls to these classes) is a bit difficult. This is where PowerMockito comes in handy.
A co-worker of mine, David, refers to PowerMock as the “circular saw in your kitchen”. 99 times out of 100 you won’t need to use it, but it does have its uses.
Anytime you’re utilizing PowerMockito, you’re going to need a class level annotation telling JUnit that we’re going to be doing some heavy lifting with PowerMock. Your test class should look something like this
If you don’t specify the PowerMockRunner, you’ll notice that all your other calls to mock out objects will not work. You get errors like
java.lang.IllegalArgumentException: Cannot subclass final class class org.eclipse.swt.graphics.Image if you don't mark your test class with this annotation.
Mocking SWT Image
Now that you have your class marked with the
@PowerMockRunner annotation, you can mock the final class
First, you need to tell PowerMock that we’re going to mock the Image class. Use the
@PrepareForTest class-level annotation for Image.
Now you can mock calls in your tests. For instance, if you wanted to mock the
Image.getBounds() call, you use the
when pattern to return an SWT
It’s as easy as that. Rinse and repeat for mocking other calls to
Mocking Static Calls to SWT Display
Mocking the SWT
Display object with Mockito is pretty straightforward. However, sometimes you’ll want to mock out the return values from static calls to
Display.getCurrent(). Here’s how you do it.
Similar to what we did with
Image, we need to prepare the
Display with the
@PrepareForTest class-level annotation.
Now, we can mock out the static calls to
Display to return another mock object. For instance, you might want to make sure some method returns the current Display.
It’s as simple as that. Happy testing!