
co.spraybot.testutils.VertxIntegrationTest Maven / Gradle / Ivy
package co.spraybot.testutils;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Verticle;
import io.vertx.core.Vertx;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import java.util.ArrayList;
import java.util.List;
/**
* A class to facilitate deploying a series of Verticles, provide a reasonable timeout for test execution, and provide
* access to the underlying Vertx and EventBus implementations used in context of the running test.
*
* @since 0.1.0
*/
@ExtendWith(VertxExtension.class)
abstract public class VertxIntegrationTest {
/**
* @return A list of Verticles you want to have deployed before your tests run
*/
abstract protected List verticles();
/**
* If you override this method in your test classes you MUST call the parent method
*
* @param vertx The Vertx instance used for the setUp context
* @param testContext The testContext that allows ensuring all Verticles are deployed before tests begin
*/
@BeforeEach
public void setUp(Vertx vertx, VertxTestContext testContext) {
List futures = new ArrayList<>();
verticles().forEach(verticle -> {
Future verticleDeployed = Future.future();
futures.add(verticleDeployed);
vertx.deployVerticle(verticle, ar -> {
if (ar.succeeded()) {
verticleDeployed.complete();
} else {
verticleDeployed.fail(ar.cause());
}
});
});
CompositeFuture.join(futures).setHandler(allDeployed -> {
if (allDeployed.succeeded()) {
testContext.completeNow();
} else {
testContext.failNow(allDeployed.cause());
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy