6

Is there a way how to unit test eclipse dialogs and SWT/Jface widgets? Or even better - is there some jar/framework which can help me mock Shell or Composite (in that way so I wouldn't have to mock too many their methods) ?

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
mawek
  • 679
  • 10
  • 19
  • Just saw your [previous question](http://stackoverflow.com/questions/12820367/eclipse-gui-testing-tool). Please elaborate and explain how this question is different and what exactly you want to know. – Baz Oct 16 '12 at 08:43
  • The previous quiestion was about GUI (integration) testing aka macro record/playback. Since I couldn't find any suitable tool for doing that I'm thinking about deeper unit testing my dialogs/widgets so this question is about unit testing some widgets and their methods - mocking swt widgets and so. – mawek Oct 30 '12 at 20:32

2 Answers2

2

You are looking for a framework to mock the SWT controls. This is generally considered best practice when using TDD or when your tests must be reliable.

SWTBot does not mock SWT. When using SWTBot, the target code executes using the standard SWT implementation for the host OS. SWTBot inspects and triggers the SWT controls from another thread. This approach does not isolate the code from the environment and tests may give different results depending on the host platform and other environmental differences that affect SWT.

Unfortunately general purpose mock libraries such as Mockito or JMockit just can't be used to mock SWT due to a combination of a lack of injectable constructors, code that validates packages, hierarchies, and final classes and methods. For a library such as SWT you will need to use custom stubs. A search does not show any SWT mock libraries. I have therefore started one at https://github.com/westbury/SWT-Mock.

Nigel W
  • 21
  • 4
  • 1
    Just came across this answer because I was looking for something along the lines of the OP. Had a look at your SWT-Mock project. The downside is that you have to change a whole lot of code in your existing project. Have you considered following another approach? SWT relies on platform-specific bundles to implement SWT. How about creating a similar (headless-mocking) SWT bundle replacing the platform-specific actual bundle and test against that? The benefit would be a fully customizable, but binary-compatible replacement ... – Stefan Winkler May 20 '14 at 06:43
  • For anyone landing here today. Static SWT controls can now be easily mocked with Mockito 3.4+. https://javadoc.io/static/org.mockito/mockito-core/3.8.0/org/mockito/Mockito.html#static_mocks – Skillzore Mar 17 '21 at 12:01
1

SWTBot should be the first thing to look at.

Here is a great tutorial from Vogella on how to use SWTBot.

Finally, here is another SO question about SWTBot.

Community
  • 1
  • 1
Baz
  • 36,440
  • 11
  • 68
  • 94