How can I make JUnit Test wait?

I have a JUnit test that I want to wait for a while synchronously. My JUnit test looks like this:

@Test public void testExipres(){ SomeCacheObject sco = new SomeCacheObject(); sco.putWithExipration("foo", 1000); //WAIT FOR 2 SECONDS assertNull(sco.getIfNotExipred("foo")); } 

I tried Thread.currentThread (). wait (), but it throws an IllegalMonitorStateException (as expected). Is there any trick, or do I need another monitor?

+70
java thread-safety junit
Apr 10 '13 at
source share
7 answers

How about Thread.sleep(2000); ? :)

+109
Apr 10 '13 at
source share

Thread.sleep () can work in most cases, but usually, if you wait, you really expect a certain state or condition to occur. Thread.sleep () does not guarantee that everything you expect actually happened.

If you expect a vacation request, for example, maybe it usually returns after 5 seconds, but if you set your sleep within 5 seconds a day, when your request returns after 10 seconds, your test will fail.

To fix this, JayWay has an excellent utility called Awatility , which is ideal for ensuring that a specific condition occurs before moving on.

He also has a nice fluent api

 await().until(() -> { return yourConditionIsMet(); }); 

https://github.com/jayway/awaitility

+49
May 27 '15 at 2:07
source share

You can also use the CountDownLatch object, as described here .

+4
Feb 02 '16 at 21:12
source share

In case your static code analyzer (for example, SonarQube) complains, but you cannot think of a way other than sleep, you can try to hack like: Awaitility.await().pollDelay(Duration.ONE_SECOND).until(() → true); This is conceptually wrong, but it's the same as Thread.sleep(1000) .

The best way, of course, is to pass Callable with your appropriate state, and not with true , like mine.

https://github.com/awaitility/awaitility

+2
Aug 02 '18 at 10:01
source share

You can use the java.util.concurrent.TimeUnit library, which internally uses Thread.sleep. The syntax should look like this:

 @Test public void testExipres(){ SomeCacheObject sco = new SomeCacheObject(); sco.putWithExipration("foo", 1000); TimeUnit.MINUTES.sleep(2); assertNull(sco.getIfNotExipred("foo")); } 

This library provides a clearer interpretation for a unit of time. You can use “HOURS” / “MINUTES” / “SECONDS”.

+2
Jun 05 '19 at 9:55
source share

using Java using wait and notify:

  @Before public void setup() { DataCache.setWhenReady(() -> { synchronized (this) { notify(); } }); try { synchronized (this) { wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } 
0
May 01 '19 at 4:53
source share

There is a common problem: it is difficult to scoff at time. Also, it’s really bad practice to put long running / waiting code in a unit test.

So, to create the test scheduling API, I used an interface with a real and dummy implementation like this:

 public interface Clock { public long getCurrentMillis(); public void sleep(long millis) throws InterruptedException; } public static class SystemClock implements Clock { @Override public long getCurrentMillis() { return System.currentTimeMillis(); } @Override public void sleep(long millis) throws InterruptedException { Thread.sleep(millis); } } public static class MockClock implements Clock { private final AtomicLong currentTime = new AtomicLong(0); public MockClock() { this(System.currentTimeMillis()); } public MockClock(long currentTime) { this.currentTime.set(currentTime); } @Override public long getCurrentMillis() { return currentTime.addAndGet(5); } @Override public void sleep(long millis) { currentTime.addAndGet(millis); } } 

In doing so, you can simulate time in your test:

 @Test public void testExipres() { MockClock clock = new MockClock(); SomeCacheObject sco = new SomeCacheObject(); sco.putWithExipration("foo", 1000); clock.sleep(2000) // WAIT FOR 2 SECONDS assertNull(sco.getIfNotExpired("foo")); } 

Of course, the advanced multi-threaded layout for Clock much more complicated, but you can do this, for example, using ThreadLocal links and a good time synchronization strategy.

0
Jul 02 '19 at 13:37
source share



All Articles