Run junit test certain amount of time


I want to test something for a while, say 5 seconds, and then pass the test if nothing wrong has been asserted. Is this possible with annotations? Can something like @Test(uptime=5000) be used?


<strong>Revised answer after question was edited</strong>

Fundamentally it feels like you're testing the wrong thing here - it seems very odd for "nothing happening" to be a sign of success.

If you want to prove that your algorithm can run for a certain amount of time without failing, I would actually extract out a single cycle, then write a test of something like:

@Test public void fineForFiveSeconds() { long start = System.nanoTime(); long end = start + TimeUnit.SECONDS.toNanos(5); while (System.nanoTime < end()) { test.executeOneIteration(); } }

This way you don't have a separate thread which has to kill the <em>working</em> code, etc.

<strong>Original answer</strong>

This answer was written <em>before</em> the question indicated that timing out was a sign of success, not failure.

I think you just want the <a href="http://junit.sourceforge.net/javadoc/org/junit/Test.html#timeout%28%29" rel="nofollow">timeout</a> attribute in the @Test annotation:

@Test(timeout = 5000)

with documentation:


Optionally specify timeout in milliseconds to cause a test method to fail if it takes longer than that number of milliseconds.



  • Unit testing a method with easymock
  • EasyMock - mock object returned from new Object
  • Selenium WebDriver setting ImplicitlyWait does nothing
  • I've been taught not to place most methods in a general “System” class but where do they go ins
  • Copy a file block per block in C
  • Why would I extend java.lang.RuntimeException
  • One Definition Rule: Can corresponding entities have different names?
  • Play Framework nested form errors missing
  • Download/Save/Write a file on the client's hard disk using flash/flex
  • CSS: How to fix overlapping divs
  • NativeQuery Spring Data return object
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Regex for nested values
  • How gzip file gets stored in HDFS
  • Time out Error in send mail
  • ApplePay PKPaymentAuthorizationViewController always shows processing
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Ajax calls do not work in IE unless you fiddle with security settings
  • Time complexity of a program which involves multiple variables
  • one Local Olampyad Questions on Informatic in 2011
  • Get data from AJAX - How to
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Bug in WPF DataGrid
  • How to add date and time under each post in guestbook in google app engine
  • How to recover from a Spring Social ExpiredAuthorizationException
  • TFS: Get latest causes slow project reloading
  • angularjs unit test when to use $rootScope.$new()
  • Controls, properties, events and timers running in design time
  • Does CUDA 5 support STL or THRUST inside the device code?
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How do I rollback to a specific git commit
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]