2

I need to determine whether or not a particular method of a class is called when it receives a kill signal. This method, where it performs some cleanup actions, is called in a shutdown hook.

I tried using EasyMock to expect that this method is called after executing a Process that starts the main method of this class and then call the Process destroy method as part of the test's action, but somehow this class's mock is not connected to the process I launch.

How can I test that a class's method is called upon receiving a shutdown signal? I'd like to do this without having to modify the class's implementation in order to test the class.

guerda
  • 23,388
  • 27
  • 97
  • 146

1 Answers1

1

The JVM is responsible for executing shutdown hooks that have been registered with it. As you are not the developer of the JVM, you have to trust it does that correctly. So you would want to test that your shutdown hook has been registered. You can do that by examining the return value of removeShutdownHook. But there is a practical snag: you probably don't want to run your shutdown hook when JUnit exits, so you should not register a shutdown hook in unit tested code. So I don't think you can.

The best you can do is the unit test the code that is used by your shutdown hook.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • 1
    See the somewhat related problem oftesting methods that exit the application:http://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit – Raedwald May 25 '13 at 12:20