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.
There are number of unit tests which set up test data in the database before they start. (I know there are lots of reasons not to do that, but when an application has as few unit tests as this one does you hang on to whatever you’ve got.) One of these tests passed consistently with Java 6, but failed with Java 8. The failure suggested a problem with the test data.
Here’s a snippet of the code. The methods are the traditional ones from the JUnit 3 days. In the debugger I could see that data was being created successfully by the setup method, so how could there be a problem with it?
@Override @Before public void setUp() { // initialization code... // more initialization code... } @Override @Before public void tearDown() { super.tearDown(); }
It turned out that the problem was right in front of me but hard to spot: the tear down method was annotated with @Before. What happens when there are two @Before methods? If the tear down runs before the setup it’s okay, there’s just nothing actually deleted. But if the tear down runs after the setup it cleans out all the test data just before the test runs.
How this code ran reliably with Java 6 I do not know. To fix it for Java 8, I actually just removed the tear down method since (as you may have noticed) it didn’t do anything useful anyway.