null ptr during unit testing

I'm running into this problem when running my unit test. The code causing this is just creating a notification:

java.lang.NullPointerException was thrown.
java.lang.NullPointerException
    at com.intellij.notification.Notifications$Bus.notify(Notifications.java:66)
    at com.intellij.notification.Notifications$Bus.notify(Notifications.java:62)
    at org.scalaideaextension.eventlog.EventLog$.info(EventLog.scala:28)
    at org.scalaideaextension.eventlog.EventLog$.info(EventLog.scala:17)
    at org.kostaskougios.idea.scripts.ScriptsManager$$anonfun$2.apply(ScriptsManager.scala:47)

This is the failing code:

      if (ApplicationManager.getApplication().isUnitTestMode()) {
...

ApplicationManager.getApplication() is null

I setup my env like this:

    val factory = IdeaTestFixtureFactory.getFixtureFactory
    val builder = factory.createFixtureBuilder("ScriptsManagerSuite")
    val insightFixture = factory.createCodeInsightFixture(builder.getFixture)
    insightFixture.setTestDataPath("/tmp/ScriptsManagerSuite")
and then test my code which in turn works ok up to the point where it creates a notification:

        val notification = new Notification("scala-idea-extensions", title, msg, NotificationType.INFORMATION)
        Notifications.Bus.notify(notification)

Any ideas how to create the application during initialization of my tests?

Thanks

3 comments
Comment actions Permalink

it seems the testing API of intellij works only if it runs with some junit runner. To resolve this issue, and since I run my tests using scala test, I had to do the following:

- create this class that extends the base junit class for intellij :

class TestSetup(name: String) extends JavaCodeInsightFixtureTestCase
{

    override def getName = name

    def init() {
        setUp()
    }

    def done() {
        tearDown()
    }

    override def getHomePath = {
        val tmpDir = System.getProperty("java.io.tmpdir")
        val home = new File(tmpDir, "idea-tests-home-path")
        home.mkdir()
        home.getAbsolutePath
    }
}

- from my scalatest suites I had to init() and done() that class:

    var setup: TestSetup = _

    override protected def beforeAll() {
        setup = new TestSetup(getClass.getSimpleName)
        setup.init()
    }

    override protected def afterAll() {
        setup.done()
    }

- my scalatest has to run via the junit runner :

@RunWith(classOf[JUnitRunner])



Now it works, a workaround but couldn't find a better way.

0
Comment actions Permalink

Hi Konstantinos,

You don't have to run your tests under JUnit - I do all my testing with the fixtures. Here's my init code - it's in Clojure, but hopefully you can see what's going on well enough:

defn idea-test-fixture [& {:as args}]
  (fn [f]
    (BasicConfigurator/configure)
    (.setLevel (Logger/getRootLogger) Level/WARN)
    (let [factory (IdeaTestFixtureFactory/getFixtureFactory)
          descriptor (proxy [DefaultLightProjectDescriptor] []
                       (getSdk []
                         (-> (JavaSdk/getInstance)
                             (.createJdk "1.7" (TestUtils/getMockJdk) false))))
          builder (.createLightFixtureBuilder factory descriptor)
          fixture (.getFixture builder)
          tmp-dir-fixture (LightTempDirTestFixtureImpl. true)
          java-factory (JavaTestFixtureFactory/getFixtureFactory)
          test-fixture (.createCodeInsightFixture java-factory fixture tmp-dir-fixture)]
      (if-let [inspections (:inspections args)]
        (.enableInspections test-fixture ^"[Ljava.lang.Class;" (into-array Class inspections)))
      (.setUp test-fixture)
      (try
        (if-let [libraries (:libraries args)]
          (doseq [library libraries]
            (add-library (.getModule test-fixture) library)))
        (binding [*fixture* test-fixture]
          (if-let [files (:files args)]
            (doseq [[path text] files]
              (add-file path text)))
          (if-let [classes (:classes args)]
            (doseq [text classes]
              (.addClass test-fixture text)))
          (f))
        (finally
          (.tearDown test-fixture))))))
0
Comment actions Permalink

Thanks, my code is similar, maybe without the

DefaultLightProjectDescriptor


But are you running this code using the junit runner?

0

Please sign in to leave a comment.