Sep 30, 2005

Writing unit tests for IntelliJ IDEA plugins

According to some old saying, if code can be broken, it should be tested. Unfortunately, writing tests for IntelliJ IDEA plugins is not that obvious. I've spent some noticable time trying to setup enviroment for IDEA-specific code in the IDEtalk plugin and now I'd like to share what I've learned.

First of all, if you have a chance to write isolated tests, that don't require implementation of IDEA interfaces from OpenAPI - just do it. Such tests will be much faster, because they won't require ~10 seconds of setUp() execution.

But if you really have to write integration tests (in my case, I wanted to be sure that IDEtalk's internal file representation is syncronized with IDEA's VirtualFiles) - welcome to IDEA's TestCases. IDEA's distribution contains a number of base TestCase classes, placed in the com.intellij.testFramework package. The most noticeable of them are IdeaTestCase and LightIdeaTestCase. The lightness of LightIdeaTestCase springs from the fact, that tests derived from this class share the same virtual IDEA application and opened project. So, after the long startup of the first test, subsequent ones will run quite fast. IdeaTestCase - derived tests share only the application, and each test run includes the overhead of another IDEA project setup.

Guess, which base class is recommended for your tests?

OK, you've chosen the base TestCase and now eager to run your tests. Not so fast. Please make sure that:
  1. tools.jar from your JDK is in your classpath
  2. all jar files from $IDEA_HOME/lib are in your classpath
  3. you've added -Didea.plugins.load=false -Xbootclasspath/p:$IDEA_HOME/lib/boot.jar to your VM parameters. The first parameter can be skipped, but in this case you'll have to add all IDEA plugins to classpath.
So, now can get your tests working.
Good luck :)