0

I am writing unit test cases for a project that will take JSON as post body and respond using JSON post body.

There are multiple checks to be made like:

  1. Are all mandatory fields specified?
  2. How is the system behaving with null values or field absence?
  3. How is the system behaving with null values in inner jsons?
  4. Boundary cases for numeric attributes.

For my initial set of unit test cases, I wrote multiple json's manually, store them as flat files and ran test cases. This will not scale out well for so many micro services.

So I want to achieve most of it using code. So in my initial phase I want to generate all permutation of json object given a skeleton. Like remove fields, make fields null, etc.

I am using Java 1.8, Jackson faster xml, jersey to achieve this. I am stuck at multiple fronts here:

  1. Since this is a very common use case, am I unaware any specific tool?
  2. Will Jackson JsonNode equals() method identify inequality with field value null or field absence.
  3. What could be the impact on inner json nodes?
UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
  • Can you clarify what is your exact requirement, Till how much you are able achieve the requirement, etc. From the post not able to identify exact requirement. – Vinayak Dornala Jun 24 '15 at 18:04
  • You're asking 3 questions here where 1 is too broad, you should create 3 and show what you tried more precisely – UBIK LOAD PACK Jun 24 '15 at 19:42

1 Answers1

0

For unit testing you need to define your expectation before you throw random requests at your service. I suggest you break up the problem into discrete issues.

  1. Use e.g. http://jsonschema.net/#/ to define a schema and validate all requests against the schema using e.g. https://github.com/fge/json-schema-validator. This will ensure requests are well-formed befoer they get processed.

  2. Write specific UNIT tests for individual methods handling individual requests to test specific use cases. Use object builders to construct and queue test cases as shown e.g. at http://blog.jayfields.com/2009/01/most-java-unit-tests-consist-of-class.html

  3. You can automate generating some of the test cases. Edge cases and null values are always a good idea but you need to define your expectations first. For completely randomized unit tests read up on the discussion at Random data in Unit Tests?

What you're trying to do is more of an integration test. The validation will help weed out most of the malformed requests and as to how your system processes specific requests you should have the answers already before you started coding.

Community
  • 1
  • 1
b7kich
  • 4,253
  • 3
  • 28
  • 29