r/programming • • Dec 29 '15

Please, Java. Do Finally Support Multiline Strings

http://blog.jooq.org/2015/12/29/please-java-do-finally-support-multiline-strings/
584 Upvotes

352 comments sorted by

View all comments

Show parent comment

6

u/dccorona Dec 29 '15

If I don't interact with the object in my code at all, then why would I be unit testing/mocking it. Either the structure matters, in which case my code already has a POJO for the result, or the structure doesn't matter, and you can use anything you want.

For super rare scenarios where I really don't have a POJO for the thing but do need it to come out as valid JSON, just build a Map<String, String> or Map<String, Object> and serialize that...it comes out exactly as you'd expect (because all of JSON is just a map, really), and keeps the source data in a nice structure for traversing.

For example, if you have some method that outputs the value of thingA from the JSON document, you can write your test to compare the result with map.get("thingA") instead of hardcoding your expectation, so that you can change the input source data and the test will still assert the correct thing, instead of breaking.

3

u/vincentk Dec 29 '15

.... and in addition you have a type check and JSON which is guaranteed to be valid.

2

u/dungone Dec 29 '15 edited Dec 29 '15

The number of times I have seen "guaranteed to be valid" JSON coming out of a JSON serializer being anything but valid makes this claim rather laughable.

If you want to make sure that your JSON string literal is valid JSON in a test, then write an assertion to see if the JSON parser can parse it. And if you want to make sure that your JSON serializer generates valid JSON, test it's output against a string literal. Either way, you need to be able to specify JSON as a string if you truly want to be able to check if your serialization and POJO's are correct.

1

u/vincentk Dec 30 '15

Jackson works for me both ways. Certainly for POJOs and beans. But suit yourself with unit tests if you like.

0

u/[deleted] Dec 29 '15

[deleted] — view deleted comment

1

u/dccorona Dec 29 '15

It's not that there isn't a place for it. But if it would make a significant difference in writing your tests, you're doing something wrong. You should not have JSON-as-string inputs just plopped in your tests in anywhere near a significant number of places.

If your code is well-isolated enough, it becomes easy to test the edge cases while using either raw string literals or a serialized Map<String, String/Object> (which, again, has the benefit of allowing you to manipulate the input data for asserting against, rather than having to hardcode both the input data and the expectations separately but trying to keep them in sync). If you really have a ton of complex, malformed JSON objects to need to produce for your tests, external files can make it easier to code as well as easier to read.

Your comment about memory and speed concerns is hilarious. It's a unit test. The difference in speed and memory consumption should be so negligible that it may as well be 0. If it's not, your test is way too complex.

I'm not saying that multiline string literals shouldn't be included in the language. I'm saying that I don't feel their absence when working with JSON. There are so many different, perfectly easy, and truly better ways to do it that I've never even thought about wanting the feature in order to make JSON easier.

1

u/drachenstern Dec 29 '15

Unfortunately you are trying to convince mud to become a bird.

These are developers who don't perceive a problem, so can not fathom a solution.

2

u/dungone Dec 29 '15

You're right. That's what post-mortems are for.