JSON modular output module, best practices

I am currently working on a module that takes one of our business objects and returns a json representation of this object to the caller. Due to limitations in our environment, I cannot use any existing json writer, so I wrote my own, which is then used by the author of the business object to serialize my objects. By json tested similarly to this

@Test public void writeEmptyArrayTest() { String expected = "[ ]"; writer.array().endArray(); assertEquals(expected, writer.toString()); } 

which is controlled only by the small output generated by each instruction, although I feel that there should be a better way.

The problem I'm currently facing is writing tests for the object writer module, where the result is much larger and much less manageable. The risk of spelling errors in the expected lines dropping my tests seems too great, and writing code in this way seems silly and unmanageable in the long run. I constantly feel that I want to write tests to make sure my tests are behaving correctly, and this feeling bothers me.

Which is better for this?

+6
json unit-testing
source share
2 answers

Technically, the environment in which you deploy your code is different from the environment in which you develop it, so I would use an existing JSON reader / writer to verify what you created. If you use maven, you can even set the scope of the JSON package that you decide to use to be a "test", so it will not be included in the actual assembly.

 <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20090911</version> <scope>test</scope> </dependency> 

Alternatively, you can inherit unit test to your JSON-writer. Inheriting, you can test all protected and private internal bits instead of checking the actual output of the string.

+5
source share

Since you cannot use external libraries in your server code, probably the best approach would be to limit api block tests to substantial ones: - Serialization of primitive primitive objects - Serialization of a test array - Serialization of special characters ...

Then move some of the tests to the client side (using the JSON parser to make sure that at least your JSON is valid). Once you find an error when executing browser scripts, correct it and write a related unit test to make sure that it does not appear again in future versions.

+2
source share

All Articles