ElFileBody is not resolving dynamic value which is a result of a function called from another class

Sorry if the title seemed confusing. I wanted to be precise in the issue since there are many threads with “ElFileBody is not resolving dynamic values” as the issue.

So here’s what I’m trying to do:
I have a ‘test asset’ class in which I have written few useful functions that are gonna get referenced by my simulations, in which one of them is random email generator - generates unique and random emails.

In my main simulation, I’m creating new object to that class and calling that method, storing the returned email to a variable called username. I’m also printing that email to the console to make sure that function is actually returning the email string.

Now I have a request body as a .txt file, in which there is a json string like: {“userName":"john@doe.com”,“code”:null}. I want to replace the user name present in this .txt file with the username that is generated by the function. Therefore I’m using this syntax:

.body(ElFileBody(“ValidateUserAlreadyExists.txt”)).asJSON))

where ValidateUserAlreadyExists.txt has - {“userName”:"${username}",“code”:null}

However during run time I get the following error: Failed to build request : No attribute named ‘username’ is defined

Note: However, instead of request body in a text file, if I use this: .body(StringBody(s"""{ “userName”:"${username}", “code”:null}""")).asJSON)) it works just fine.

Any help is appreciated…

gatling-maven-plugin - 2.2.4
gatling-highcharts-maven-archetype - 2.3.1

scala - 2.12.3
gatling and gatling-charts-highcharts - 2.3.0
scala-maven-plugin - 3.2.2

apache-maven - 3.5.3