Help in writing unit tests



I'm new to plugin development, and I'm looking for help in writing unit tests for actions & activities.

For example, how would I write a unit test for this very simple action (a file picker and message dialog):

public class FileAccessAction extends AnAction {
    public void actionPerformed(@NotNull AnActionEvent e) {
        VirtualFile vFile = getFile(e.getProject());
        if (vFile != null) {
            Messages.showMessageDialog(e.getProject(), "Success", "File Selection", Messages.getInformationIcon());
        } else {
           Messages.showMessageDialog(e.getProject(), "Fail", "File Selection", Messages.getErrorIcon());

    private VirtualFile getFile(Project project) {
        FileChooserDescriptor fileChooser = new FileChooserDescriptor(true, false, false, false, false, false);
        fileChooser.setTitle("Select File");
        fileChooser.setDescription("Select the file");
        return FileChooser.chooseFile(fileChooser, project, null);


Do I need to use mocks? If yes, can you please give an example.

1 comment
Comment actions Permalink


In general, you have to initiate your action and invoke your actionPerformed method.

You can find test examples in our intellij-community repository, i.e.:

AnAction action = new SomeAction();
AnActionEvent e = AnActionEvent.createFromDataContext(...);

Please sign in to leave a comment.