hackjutsu hackjutsu -4 years ago 44
Java Question

@AfterClass method not called when the test suit is terminated manually in Eclipse

I notice that the JUnit's

method is not called when the test suit is terminated manually in Eclipse. This causes some trouble in debugging since I'm using the
method for cleaning up.

Is there a way to make sure the clean up step is called in this case? (For some reason, I sometimes have to manually stop the tests...)


Answer Source

First: how does eclipse terminate a running program: https://stackoverflow.com/a/8941751/1100135

So terminating is not specific to JUnit so JUnit can't (or maybe better: shouldn't) do anything about it. So if you reeeeaalyyyy have, you have to program it yourself. You probably don't have many options: shutdown hooks or eclipse plugins. But what if other members of your team will use IntelliJ? or plain maven/gradle/ant?

The real solution is to solve two problems in your tests design. The first is that you have to kill your tests. Fix that! What if you start using CI? The second problem is that your tests have to clean up. Why? What happens if they don't? Try to write them in a way that it doesn't matter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download