Learn from the AWaitility framework in the Java class library

Learn from the AWaitility framework in the Java class library Introduction: Awaitility is a Java class library for testing asynchronous tasks, making the writing asynchronous test simple and intuitive.It provides a concise API that can be used to wait for certain conditions, and test assertions after meeting the conditions.This article will introduce the working principles, usage methods, and related code and configuration of the AWAITILITY framework. working principle: The AWAITILITY framework ensures the correct execution of the test by waiting for the satisfaction of specific conditions.It is based on chain call and uses the Fluent API method to define the waiting conditions.Awaitility Pockets the waiting condition in a function expression by using Java's LAMBDA expression and anonymous internal class.Then, it repeats and regularly check this function expression until the conditions are met or reached timeout. Instructions: 1. Introduce dependencies In the project construction configuration file (such as pom.xml), add the dependency item of AWaitility.For example: <dependency> <groupId>org.awaitility</groupId> <artifactId>awaitility</artifactId> <version>4.0.3</version> <scope>test</scope> </dependency> 2. Import the necessary classes In the test class, the related class of the AWaitility framework is introduced.For example: import static org.awaitility.Awaitility.*; import static org.awaitility.Duration.*; import static org.awaitility.pollinterval.*; import static org.hamcrest.Matchers.*; 3. Write test code Use AWaitility for waiting conditions and assertion.For example, the following example shows the situation where the number of elements in a list reaches the specified value: @Test public void testListSize() { List<String> list = new ArrayList<>(); ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.submit(() -> { // Add elements to the list after a period of time Thread.sleep(1000); list.add("Element 1"); list.add("Element 2"); }); // The number of waiting list elements reaches 2 await().atMost(5, SECONDS).until(list::size, equalTo(2)); // Eclabo the elements in the list assertThat(list, contains("Element 1", "Element 2")); } Explanation code and related configuration: -Line 1: Create an empty list to store elements. -Line 3: Create a single -threaded EXECUTORSRVICE for simulation asynchronous tasks. -Line 4-10: Submit an asynchronous task to ExecutorService by using Lambda expressions. The task adds elements to the list after a period of time. -Watch 13: Use Awaitility to wait until the number of list elements reaches 2. -Wly 15: Is the element in the list conforms to expectations.In this example, we assert that the list contains "Element 1" and "Element 2". -17: Complete the test. In actual use, more configuration and customization can be performed according to the needs, such as setting timeout time, rotating interval, custom waiting conditions, etc. Summarize: By understanding the Awaitility framework in the Java class library, we have learned its working principles and usage methods.The AWAITILITY framework simplifies the test of asynchronous tasks and provides easy -to -use API to define the waiting conditions and assertions.Through reasonable configuration and use, you can ensure that reliable asynchronous testing and better cope with concurrent scenes.