Play 2.4 Messages Throwing a NullPointerException

This problem occurs only in test mode. Basically, I have several Call.get () calls in the tested class

Messages.get("api.parser.missing.id")

It exists in

public long getParserId() throws UnknownParserIdException {
        if (parserEntity == null || parserEntity.parserId == 0) {
            throw new UnknownParserIdException(Messages.get("api.parser.missing.id"));
        }
        return parserEntity.parserId;
    }

In my test, I have the following

@Test
public void testSaveAndGetParserId() {

    try {
        addParserService.attachAppId(appId);
        addParserService.attachParserJson(jsonToAttach);
        addParserService.transformAttachedJson();
        addParserService.saveParserEntity();
        Long parserId = addParserService.getParserId();
        boolean isPositive = parserId > 0;
        assertTrue(isPositive);
        ParserRepository parserRepository = new ParserRepository();
        parserRepository.deleteParserById(parserId);
    }
    catch (MalformedParserException | UnknownAppIdException | UnknownParserIdException e) {

        assertTrue(false);
    }
}

Now when I run this test, I get the next NPE

[error] Test services.AddParserServiceTest.testTransformNoJsonAttached failed: java.lang.NullPointerException: null, took 0.09 sec
[error]     at play.api.i18n.DefaultMessagesApi.preferred(Messages.scala:482)
[error]     at play.api.i18n.DefaultMessagesApi.preferred(Messages.scala:488)
[error]     at play.i18n.MessagesApi.preferred(MessagesApi.java:128)
[error]     at play.mvc.Http$Context.messages(Http.java:173)
[error]     at play.mvc.Http$Context.lang(Http.java:165)
[error]     at play.i18n.Messages.getLang(Messages.java:29)
[error]     at play.i18n.Messages.get(Messages.java:109)
[error]     at services.impl.AddParserService.transformAttachedJson(AddParserService.java:31)
[error]     at services.AddParserServiceTest.testTransformNoJsonAttached(AddParserServiceTest.java:57)

I have other classes and tests that are not part of my service package that are great for il8n classes.

Things I checked

  • The message being called exists in messages
  • The message file is specified in application.conf (which is included by application.test.conf)
  • Removing the Message.get () call in my service class fixes the NPE, but I don't want to do this obviously.
+4
1

, , . , , , .

: play2 unit test

+1

Source: https://habr.com/ru/post/1621478/


All Articles