• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

In Junit how to capture Thread.sleep(10000) InterruptedException without adding thorw to method

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all
i have a simple threaded application which simulates thread timeout, I'm using Thread.sleep(10000)  in the thread to mimic the thread work ,
But i allso want to test this senariue in Junit .
but i can't find away to capture this in junit without adding throw in the method or the worker .



I'm causing this thread to be killed with a small timeout with:



I don't want to add exceptions to all the program hierarchies.

The exception I'm getting:




I guess assertThatThrownBy not going to work here as this exception not thrown
is there any way in jUnit to capture this?
Thanks
 
Sheriff
Posts: 22271
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In JUnit 5 you can use assertDoesNotThrow:

This assertion turns any exception thrown from the execution into an assertion failure, thereby removing the need to catch or these exceptions or add them to your throws clause.
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rob Spoor
Thanks
Sorry but i didn't understand how to use it for my case can you give short example?
I what to detect the timeout not swallow them ...
Thanks
 
Rob Spoor
Sheriff
Posts: 22271
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JUnit 5 has assertions for that as well: assertTimeout and assertTimeoutPreemptively. See https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/Assertions.html for more information.
 
reply
    Bookmark Topic Watch Topic
  • New Topic