File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Unit Testing InterruptedException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Unit Testing InterruptedException" Watch "Unit Testing InterruptedException" New topic

Unit Testing InterruptedException

aemilia Hunter

Joined: Jan 05, 2012
Posts: 2

Hello everyone! This is going to be my first question here in this forum and I don't know if this question has already been raised way back here in codeRanch.

I wanted to know what are the best approaches in testing InterruptedException.
Let's take for example this code:

Thanks in advance!

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Welcome to CodeRanch!

I'm going to move this to our Threads forum since it requires knowledge about how to interrupt a thread.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

I'm not sure what you're looking for with "best approaches", but as far as making it work, all you have to do is call the interrupt() method on the Thread that's running that code.

Can you be more specific about what you're looking for or what difficulty you're having?
aemilia Hunter

Joined: Jan 05, 2012
Posts: 2

my code goes this way:

and my test goes this way:

Now, I am not sure if what I am doing is right or wrong, calling interrupt() before calling processThread() method.
When running the test method using djunit in eclipse, it is weird that there is no code coverage report. I am not
sure if this is a problem in eclipse or djunit.

I agree. Here's the link:
subject: Unit Testing InterruptedException
jQuery in Action, 3rd edition