Help in writing unit tests
Answered
Hello,
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 {
@Override
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.
Please sign in to leave a comment.
Anthony,
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.: AbstractFindInEditorTest.java: