How to test an action?

I defined a very simple action:

 public class MyAction extends AnAction {

     public void actionPerformed(AnActionEvent event) {
         new MyLoginDialog(event.getProject()).show();

And in `plugin.xml`, the config is:

        <group id="mypluin.actions" text="My Test Plugin" description="my test plugin">
            <add-to-group group-id="MainMenu" anchor="last"/>
            <action id="myplugin.test.action" text="_Login"/>

Which is running well when I run the plugin.

But, how can I test it?

I tried to use some base test case like:

 import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;

 public class LoginTest extends LightPlatformCodeInsightFixtureTestCase {

     public void testLogin() throws Exception {
         Presentation presentation = myFixture.testAction(new MyAction());
         // ...

But it will throw exception:

 at com.intellij.openapi.ui.DialogWrapper.invokeShow(
 at myplugin.MyAction.actionPerformed(
 at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.testAction(
 at myplugin.LoginTest.testLogin(
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(
 at com.intellij.testFramework.UsefulTestCase.access$001(
 at com.intellij.testFramework.UsefulTestCase$

I'm not sure if I use correct base class and correct test method, could you give me some suggestions?

Comment actions Permalink

The IntelliJ test framework does not have any support for testing UI code such as dialogs. We find it much more valuable to test the underlying logic behind the UI.

Comment actions Permalink

You are right. But some of my UI also have complex logic, I don't feel safe if I don't test them at all. Is there any way to test them?

Comment actions Permalink

You can test the logic by extracting it into classes that don't depend on Swing or IntelliJ UI components.


Please sign in to leave a comment.