Finatra FeatureTests: How to manually deserialize returned json

249 Views Asked by At

I read the Finatra getting started guide and I was able to write the HelloWorld Service and its feature test.

Currently my feature test looks like

server.httpPost(
   path = "/hi",
   postBody = """{"name": "Foo", "dob": 136190040000}""",
   andExpect = Ok,
   withBody = """{"msg":"Hello Foo. You are 15780 days old today"}""")

This works fine and my tests pass. However my requirement is that I extract the json returned by the server and then manually perform asserts on the object returned.

I changed my code to

val response = server.httpPost(
   path = "/hi",
   postBody = """{"name": "Abhishek", "dob": 136190040000}""",
   andExpect = Ok,
   withBody = """{"msg":"Hello Abhishek. You are 15780 days old today"}""")
  val json = response.contentString

This also works and I can see the json returned in side the variable json.

My question is that if I have to deserialize this json into an object. Should I just pull in any json library like circe? and then deserialize the object?

or can I use the jackson framework which comes inside of Finatra.

In all examples I could find, I see that Finatra "automatically" handles the json serialization and deserialization. But in my case I want to perform this manually.

1

There are 1 best solutions below

0
On BEST ANSWER

You can use the FinatraObjectMapper by calling (using your example) server.mapper. That wraps a Jackson ObjectMapper that you could use if you wanted to use the Jackson library without any of the Finatra add ons.

Or you can import your a different JSON library. If you are using SBT, you can restrict libraries to certain areas of your code, so if you wanted to use circe only in the test code, you could add the following to your build.sbt

"org.scalatest" %% "scalatest" % "2.2.6" % "test"