In my play-framework application, there is a messages file which I can use to define my own messages. Eg

##############Application Error messages

error.incorrectBodyType=Incorrect body type. Body type must be JSON
error.incorrectBodyContentInLoginRequest=No Login info found in the request. Check format
error.incorrectUsernameOrPassword = Incorrect username or password
error.unregisteredUser = registration not complete
error.ghostUsername = No record found for the username
error.unauthenticated = You must first sign in
error.unauthorized = You are not permitted

####### Application Success Messages
success.signoutSuccessful = Signout Successful

The play frameworks makes these messages available to my Controller and I can use them like the messagesApi("error.unauthenticated")(langs.availables(0)).

I am unable to figure out how I can use the messages file in my unit tests. I am using compile time injection and am creating my own instances of the required classes. To create MessagesApi, there is a DefaultMessagesApi method but it take a map, not a File.

I am able to create the messages in my unit tests as follows but I have to duplicate the effort of copying messages from messages file to this map

val messagesApi = new DefaultMessagesApi( //takes map of maps. the first is the language file, the 2nd is the map between message title and description
    Map("en" -> //the language file
      Map("error.incorrectBodyType" -> "Incorrect body type. Body type must be JSON",
        "error.incorrectUsernameOrPassword"->"Incorrect username or password",
        "error.incorrectBodyContentInLoginRequest"->"No Login info found in the request. Check format", //map between message title and description
        "error.unregisteredUser" -> "registration not complete",
        "error.ghostUsername" -> "No record found for the username",
        "success.signoutSuccessful" -> "Signout Successful",
        "error.unauthenticated" -> "You must first sign in",
        "error.unauthorized" -> "You are not permitted")


Is there a way I can read the messages file and use it in my test case?

1 Answers

Manu Chadha On

My test case already extended OneAppPerSuiteWithComponents. I used components.messagesApi of OneAppPerSuiteWithComponents which was already available and reads from the messages file