<dependency> <groupId>org.awaitility</groupId> <artifactId>awaitility</artifactId> <version>4.1.0</version> <scope>test</scope> </dependency> public class UserService { public CompletableFuture<User> getUserById(String userId) { CompletableFuture<User> future = new CompletableFuture<>(); Thread thread = new Thread(() -> { }); thread.start(); return future; } } import org.junit.Test; import java.util.concurrent.TimeUnit; import static org.awaitility.Awaitility.await; import static org.hamcrest.Matchers.equalTo; public class UserServiceTest { @Test public void testGetUserById() { UserService userService = new UserService(); CompletableFuture<User> future = userService.getUserById("123"); await().atMost(5, TimeUnit.SECONDS) .until(() -> future.isDone() && future.get().getName().equals("John Doe")); assertThat(future.get().getName(), equalTo("John Doe")); } }


上一篇:
下一篇:
切换中文