1

I recently ran in to a weird issue where tests were failing on our Bamboo box, but not on dev boxes. After a lot of debugging we found out that the cause was due to using a == on a Long instead of .equals. Once I figured it out it made sense, but I was surprised that the no developer could recreate this, but it always happened on the build box.

For our tests it would be nice to catch these kinds of things and try to prevent == where it wasn't intended, but I am not aware of a way to tell Java don't intern Longs, Strings, etc... Is there a setting you can give to the JVM to handle this?

Zipper
  • 7,034
  • 8
  • 49
  • 66
  • No I want to change how java works when testing to try to catch bugs. – Zipper Sep 05 '13 at 16:49
  • Interning of longs, etc is not really a language feature. You can create new longs using the constructor and not the static method, but since you most certainly use auto boxing that's no solution either. The real solution is to use a static checker. That should catch such bugs. – Voo Sep 05 '13 at 16:49

2 Answers2

8

One option to detect this type of issue is to perform static analysis on the code during your build. I believe FindBugs has a detector for reference equality.

See: Suspicious Reference Comparison

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
0

A quick Google search turned up blank, but I believe that there is a command line argument to control the size of the Long cache (defaults to 256). This is something I read on StackOverflow maybe a year ago, so, it pays to follow SO! If you can find that and set it to zero on your test machine it would cover this particular problem.

Found it - look at SO question# 2974561. Answer near the end by @sunny.

New Integer vs valueOf

Hmm, looking in more detail it seems that you may increase the cache, but not decrease. So don't think this will work, but maybe you can figure something out.

Community
  • 1
  • 1
user949300
  • 15,364
  • 7
  • 35
  • 66