How to test an action?

I defined a very simple action:

 public class MyAction extends AnAction {

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

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

 <actions>
        <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"/>
        </group>
    </actions>

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:


 java.lang.NullPointerException
 at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl.show(DialogWrapperPeerImpl.java:477)
 at com.intellij.openapi.ui.DialogWrapper.invokeShow(DialogWrapper.java:1596)
 at com.intellij.openapi.ui.DialogWrapper.show(DialogWrapper.java:1553)
 at myplugin.MyAction.actionPerformed(MyAction.java:10)
 at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.testAction(CodeInsightTestFixtureImpl.java:848)
 at myplugin.LoginTest.testLogin(LoginTest.java:20)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at com.intellij.testFramework.UsefulTestCase.access$001(UsefulTestCase.java:85)
 at com.intellij.testFramework.UsefulTestCase$2.run(UsefulTestCase.java:305)

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

2

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.

0

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?

0

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

0

请先登录再写评论。