Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

HowTo: testing jFace Action class taking a StructuredSelection object

2 min read

Let's consider the following jFace Action class:
public class RateAction extends Action {
private final int fRating;
private IStructuredSelection fSelection;

* @param rating
* @param selection
public RateAction(int rating, IStructuredSelection selection) {
//hack: have to use a button because otherwise the star images won't show up on Linux (Ubuntu)
super("", AS_PUSH_BUTTON);
fRating = rating;
fSelection = selection;



* @see org.eclipse.jface.action.Action#run()

public void run() {
List<INews> newsList = ModelUtils.getEntities(fSelection, INews.class);
if (newsList.isEmpty())

/* For each News */
for (INews newsItem : newsList) {

/* Save */
Action classes are heavily used in the Eclipse plugin development environment. They are basically nothing other than the representation of the Command design pattern, which is an extremely useful pattern. For instance for factoring out/grouping common behavior. Do not always put it in a common superclass: inheritance should only be used for specialization and moreover it imposes a very strong dependency among the classes. Command objects can be easily exchanged and are much lower coupled.
But back to the main purpose of this post. Another advantage of the Command pattern is to increase testability. So the above Action class can be tested as follows. The only peculiarity in this specific case may on how to create the appropriate StructuredSelection object which is normally automatically provided by the jFace environment. But also this is quite straightforward as can be seen:
public class RateActionTest {
private RateAction action = null;
private INews newsItem = null;
private IStructuredSelection selection = null;

public void setUp() throws Exception {
//reset DB schema

//construct the necessary selection
IFeed feed = new Feed(Long.parseLong("" + 0), new URI("http://www.dummyurl.com"));
newsItem = new News(feed);
selection = new StructuredSelection(newsItem);

public void tearDown() throws Exception {
selection = null;
newsItem = null;
action = null;

* Tests the actions core parts
public void testAction(){

//Verify that the created newsItem has a rating of 0
assertEquals("rating should be equal to 0", 0, newsItem.getRating());

//boundary test: empty selection -> nothing should happen, no exception etc..
action = new RateAction(3, new StructuredSelection());

//start the Action and set some rating
action = new RateAction(3, selection);
assertEquals("rating should be equal to 3", 3, newsItem.getRating());

//decrease the rating again
action = new RateAction(1, selection);
assertEquals("rating should be equal to 1", 1, newsItem.getRating());

Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus