unit testing - How to debug something that works under Java 7 but not under Java 8 -
i have project unit test works under java 7, not under java 8. there way investigate such things? (i'm sure test correct; suggests there's subtle bug in implementation.)
really suppose quick way identify code paths diverge. hard, because there might sorts of differences in code paths @ low level through jdk, , don't want bogged down in irrelevant differences down tiny optimisations.
so nice thing maybe ask @ top level trace paths diverge; , then, starting before point, ask @ second level trace paths diverge; , on.
but i've no idea whether there's way this. fear waste lot of time if don't have systematic approach.
the code, way, apache phoenix repository, under java 8, following failure:
tests run: 1, failures: 1, errors: 0, skipped: 0, time elapsed: 0.006 sec <<< failure! - in org.apache.phoenix.schema.pmetadataimpltest testeviction(org.apache.phoenix.schema.pmetadataimpltest) time elapsed: 0.006 sec <<< failure! java.lang.assertionerror: expected:<3> was:<2> @ org.junit.assert.fail(assert.java:88) @ org.junit.assert.failnotequals(assert.java:834) @ org.junit.assert.assertequals(assert.java:645) @ org.junit.assert.assertequals(assert.java:631) @ org.apache.phoenix.schema.pmetadataimpltest.testeviction(pmetadataimpltest.java:98)
one of visible changes between java 7 , java 8 change how hash data structures handle collisions - linked lists used, entries stored in balanced trees. in cases can affect iteration order of these data structures. isn't language regression since these data structures explicitly not specify iteration order, careless code , tests can written assume order.
in rare situations, change introduce change iteration order of
hashmap
,hashset
. particular iteration order not specifiedhashmap
objects - code depends on iteration order should fixed.
so first thing when debugging java 7 -> 8 failures miss-uses of hashmap
, hashset
, , concurrenthashmap
. once you've found mistake have few choices:
- replace data structure 1 explicitly provides iteration order, e.g.
linkedhashmap
,treemap
, orimmutablemap
. - remove iteration-order assumptions production code (for instance assuming key/value first element in map).
- improve tests check specified behavior (which elements) rather unspecified behavior (what order). test data defined in easy-to-write incorrect way, using inline array syntax
int[] expected = {1,2,3}
, looping on elements. can make easier using guava's static constructors (immutableset.of(1,2,3)
) or truth's fluent assertion syntax (assertthat(someset).containsexactly(1, 2, 3)
).
if hash ordering proves not root cause issue more subtle , best option step through debugger, suggested. sure @ least skim what's new in jdk 8 clues.
Comments
Post a Comment