Let me demonstrate on a few simple but meaningful enough examples how easy it is to enrich your tests with it. Let's start with a POM file including only necessary stuff - JUnit and Awaitility.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelversion >4.0.0</ modelversion > < groupid >com.example</ groupid > < artifactid >awaitility</ artifactid > < version >0.0.1-SNAPSHOT</ version > < packaging >jar</ packaging > < properties > < project.build.sourceencoding >UTF-8</ project.build.sourceencoding > </ properties > < dependencies > < dependency > < groupid >com.jayway.awaitility</ groupid > < artifactid >awaitility</ artifactid > < version >1.3.3</ version > < scope >test</ scope > </ dependency > < dependency > < groupid >junit</ groupid > < artifactid >junit</ artifactid > < version >4.8.2</ version > < scope >test</ scope > </ dependency > </ dependencies > </ project > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.example.awaitility; import java.util.Collection; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class AsyncQueueService { private final ExecutorService executor = Executors.newFixedThreadPool( 3 ); private final ConcurrentLinkedQueue< Notification > queue = new ConcurrentLinkedQueue< Notification >(); public static class Notification { private final long userId; public Notification( final long userId ) { this .userId = userId; } public long getUserId() { return userId; } } public void enqueue( final Collection< Long > users ) { executor.execute( new Runnable() { public void run() { for ( final long userId: users ) { // do some work with notifications queue.add( new Notification( userId ) ); } } } ); } public void clear() { queue.clear(); } public int size() { return queue.size(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.example.awaitility; import static com.jayway.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; import java.util.Arrays; import java.util.concurrent.Callable; import org.junit.Before; import org.junit.Test; public class AsyncQueueServiceTestCase { private AsyncQueueService service; @Before public void setUp() { service = new AsyncQueueService(); } @Test public void testEnqueueManyNotifications() throws Exception { final Long[] users = new Long[] { 1L, 2L, 3L, 4L, 5L }; service.enqueue( Arrays.asList( users ) ); await().until( new Callable< Integer >() { public Integer call() throws Exception { return service.size(); } }, equalTo( users.length ) ); } } |
I have just touched the surface of Awaitility. It has many features and even specific DSLs for Groovy and Scala. I highly encourage to take a look on it!