Mark
Mark

Reputation: 864

NullPointerException on FakeApplication start

I'm writing unit tests for a web app and using the Java Play! (2.1.3) framework's FakeApplication class.

public class TagTest {
    public static FakeApplication app;    
...
    // BeforeClass only runs once before any tests are run
    @BeforeClass
    public static void setUpBeforeClass() {
        // Set up new FakeApplication before running any tests
        app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
        Helpers.start(app);
    }
...
}

I have the same setUpBeforeClass() method in all four of my test classes that test each of my four different models but when I run the play test command, all four test classes return a similar error:

[error] Test models.TagTest failed: java.lang.NullPointerException: null
[error]     at Global.onStart(Global.java:59)
[error]     at play.core.j.JavaGlobalSettingsAdapter.onStart(JavaGlobalSettingsAdapter.scala:17)
[error]     at play.api.GlobalPlugin.onStart(GlobalSettings.scala:175)
[error]     at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:68)
[error]     at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:68)
[error]     at scala.collection.immutable.List.foreach(List.scala:309)
[error]     at play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:68)
[error]     at play.api.Play$$anonfun$start$1.apply(Play.scala:68)
[error]     at play.api.Play$$anonfun$start$1.apply(Play.scala:68)
[error]     at play.utils.Threads$.withContextClassLoader(Threads.scala:18)
[error]     at play.api.Play$.start(Play.scala:67)
[error]     at play.api.Play.start(Play.scala)
[error]     at play.test.Helpers.start(Helpers.java:354)
[error]     at models.TagTest.setUpBeforeClass(TagTest.java:35)

Where line 35 is the line:

Helpers.start(app);

Is there something I'm doing wrong here?

Upvotes: 1

Views: 755

Answers (1)

davnicwil
davnicwil

Reputation: 30967

Looks from the stack like the NPE is thrown on line 59 of your own Global class - inside your override of GlobalSettings.onStart()?

Look/debug there to see what the issue is. Hard to suggest any more without seeing your code - maybe check this out for a basic intro to Global and how it can be used.

By the way - just a couple of additional points

  • Why is app a static variable? It should probably be an instance variable of TagTest.
  • You didn't have to point out what code was on line 35 of TagTest - this can be seen on the second-last line of the stacktrace. The first line of the trace shows you the method in which the Exception was actually thrown - the rest just show the stack of calls which led up to that method call.

Upvotes: 2

Related Questions