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
Please sign in to leave a comment.
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.
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:
Thanks, my code is similar, maybe without the
But are you running this code using the junit runner?