This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
internet detective

Joined: May 26, 2003
Posts: 30068

Welcome to CodeRanch!

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

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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.

Don't get me started about those stupid light bulbs.
subject: Unit Testing InterruptedException
Similar Threads
Not Clear about Interrupt
InterruptedException.. cause and way of prevention..
What causes a thread to be interrupted?
Thread interrupt
stopping a thread