i'm having a bit of trouble trying to write some tests for my vertx api. I'm using open-api to set the route using a scheme that i download from an api-curio schema registry. My app is working but when i tried to make tests, some of them weren't working properly. It seems like my test is executing himself before the route is actually set. How can i get my test to wait before executing himself ?
Here is the problematic part of my tests :
@BeforeEach
void deploy_verticle(Vertx vertx, VertxTestContext testContext) {
mainVerticle = new MainVerticle();
client = vertx.createHttpClient();
ConfigStoreOptions propertyFile = new ConfigStoreOptions()
.setType("file")
.setFormat("properties")
.setConfig(new JsonObject().put("path", "src/main/resources/local_config.properties"));
ConfigRetrieverOptions options = new ConfigRetrieverOptions()
.addStore(propertyFile);
ConfigRetriever retriever = ConfigRetriever.create(vertx, options);
retriever.getConfig(config -> vertx.deployVerticle(mainVerticle, new DeploymentOptions().setConfig(config.result()), testContext.succeedingThenComplete()));
}
This test isn't working properly even if my api is working perfectly :
@Test
void listEmployeTest(Vertx vertx, VertxTestContext testContext) throws Throwable {
HttpClient client = vertx.createHttpClient();
client.request(HttpMethod.GET, 8080, "localhost", "/v2.3/employes/").compose(request -> request.send()).onSuccess(body -> {
assertEquals(200, body.statusCode());
testContext.completeNow();
}).onFailure(failure -> testContext.failNow(failure));
}
CodePudding user response:
See https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html
It is meant for threaded environments, though.
CodePudding user response:
Ok so i managed to find the solution to my problem ! First i tried to use
testContext.awaitCompletion(1, TimeUnit.SECONDS)
to make the test wait for the full deployement of my routes.
After that i looked at my main Verticle code and refactored it to complete the start Promise only when every route is deployed so that when the tests starts i already know that the route are already built. So i no longuer needed to wait in my tests before trying to ping my routes.