2

I am using morphia (ODM for mongodb) in my project and write its annotations on my models.
I want to write unit tests to validate the mapping.
I can call the mapper, using morphia.getMapper().toDBObject(input) and run a test on the output.
For example, for the model

Car:

@Property("COLOR")
String color;
@Property("PlaTE")
int plate;
@Property("seats")
int seats;

I expect to receive the output:

{ "COLOR" : "value", "PlaTE" : 111, "seats" : 4 }

In order to test it, I need to create a string that represent this output, and in java it's a nightmare...

String expected = "{ \"COLOR\": \"value\", \"PlaTE\": 111, \"seats\" }";

And you can see how ugly this can get with embedded objects and arrays...

I was thinking of writing the output in a json file and read it during the test, but it kinda breaks the rules of unit testing...
Is there any convenient way of representing json objects in java to achieve a neat and clean unit tests?

itaied
  • 6,827
  • 13
  • 51
  • 86
  • http://stackoverflow.com/questions/2253750/compare-two-json-objects-in-java goes into various options you have. – aha Feb 21 '17 at 17:50
  • To be more clear, I meant to an easy way to write and read (with human eyes) the json, like in JavaScript. I guess no other way than separate json file... – itaied Feb 21 '17 at 17:53
  • Well, since the answers to both (pretty similar) questions are "no", then I guess it is a duplication... – itaied Feb 21 '17 at 18:09
  • Take a look at [Karate](https://github.com/intuit/karate), it has everything you are looking for and more. (disclaimer: I am the dev). – Peter Thomas Feb 25 '17 at 03:56
  • @aha IMO this is NOT a duplicate. The reason is this question is specific to testing JSON in Java. – Peter Thomas Feb 25 '17 at 03:57

1 Answers1

-1

If you're looking for a way to test your RESTful endpoints (JSON or XML), I would recommend http://rest-assured.io/ I've used it in a number of projects and it's great. They have excellent documentation too: https://github.com/rest-assured/rest-assured/wiki/Usage

Brian Pipa
  • 808
  • 9
  • 23