Recently I’ve been working towards migrating a legacy application from Java 6 to Java 8. While the language and the JVM are backwards compatible, there have still been a few issues that I thought I would document in the hope that it might help someone else.
The biggest issue I’ve seen is unit test failures caused by unspecified sort ordering – the order in which items are returned from a collection when there isn’t an explicit ordering. Somewhere along the way, the order in which items are returned by the java.util classes when there isn’t an explicit sort order changed. With Java 6 results were returned in an order that was consistent, test run after test run. With Java 8 the order has changed. (It may still be consistent from run to run – I haven’t tested that.)
For example, one unit test iterated over the items in a Set to verify their contents. This worked in Java 6, repeatably. When run with Java 8 the same items are returned, but in a different order, so the verification failed. I had to revise that test to not depend on the order of items returned by the iterator.
Another test sorted items by timestamp, but assigned a number of them the same timestamp. That was fixed by tweaking the timestamps to have different values in the seconds field.
Then there was one where there was a bug in a production comparator: when the current object was not null and the other object was null, it treated them as equal despite a unit test which clearly expected nulls at the end. Somehow this worked with Java 6. In that case I didn’t change the unit test at all, just fixed the comparator.