Writing Junits test class for a DoubleClickListene

2019-09-07 00:20发布

问题:

I need to write a Junit test class for my DoubleClickImplementation class. Here is the code :

DoubleClickImplementation.java

public class DoubleClickImplementation implements IDoubleClickListener {
private TreeViewer treeViewer;
public DoubleClickImplementation(TreeViewer viewer) {
this.treeViewer = viewer;
}
/**
* Expands and Collapses the tree items.
* 
* @param event
* 
*/
@Override
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
if (selection == null || selection.isEmpty()) {
return;
}
Object item = selection.getFirstElement();
ITreeContentProvider provider = (ITreeContentProvider) treeViewer.getContentProvider();
if (!provider.hasChildren(item)) {
return;
}
if (treeViewer.getExpandedState(item)) {
treeViewer.collapseToLevel(item, AbstractTreeViewer.ALL_LEVELS);
} else {
treeViewer.expandToLevel(item, 1);
}
}
}

And this is how I instantiate DoubleClickImplementation class from another class named RepoView

private TreeViewer browseTreeViewer;
private DoubleClickImplementation doubleClickExpansionListener;
doubleClickExpansionListener = new DoubleClickImplementation(browseTreeViewer);
browseTreeViewer.addDoubleClickListener(doubleClickExpansionListener);

Please give me a Junit test class for the above mentioned DoubleClickImplementation class's doubleClick() method. I'm completely new to JUnits.

标签: junit swt