-
-
Save jponge/3c04d422f0969da04d0283859fefd5e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@DisplayName("👋 A fairly basic test example") | |
@ExtendWith(VertxExtension.class) | |
class SampleVerticleTest { | |
@Test | |
@DisplayName("⏱ Count 3 timer ticks") | |
void countThreeTicks(Vertx vertx, VertxTestContext testContext) { | |
AtomicInteger counter = new AtomicInteger(); | |
vertx.setPeriodic(100, id -> { | |
if (counter.incrementAndGet() == 3) { | |
testContext.completeNow(); | |
} | |
}); | |
} | |
@Test | |
@DisplayName("⏱ Count 3 timer ticks, with a checkpoint") | |
void countThreeTicksWithCheckpoints(Vertx vertx, VertxTestContext testContext) { | |
Checkpoint checkpoint = testContext.checkpoint(3); | |
vertx.setPeriodic(100, id -> checkpoint.flag()); | |
} | |
@Test | |
@DisplayName("🚀 Deploy a HTTP service verticle and make 10 requests") | |
void useSampleVerticle(Vertx vertx, VertxTestContext testContext) { | |
WebClient webClient = WebClient.create(vertx); | |
Checkpoint deploymentCheckpoint = testContext.checkpoint(); | |
Checkpoint requestCheckpoint = testContext.checkpoint(10); | |
vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> { | |
deploymentCheckpoint.flag(); | |
for (int i = 0; i < 10; i++) { | |
webClient.get(11981, "localhost", "/") | |
.as(BodyCodec.string()) | |
.send(testContext.succeeding(resp -> { | |
testContext.verify(() -> { | |
assertThat(resp.statusCode()).isEqualTo(200); | |
assertThat(resp.body()).contains("Yo!"); | |
requestCheckpoint.flag(); | |
}); | |
})); | |
} | |
})); | |
} | |
@DisplayName("➡️ A nested test with customized lifecycle") | |
@Nested | |
class CustomLifecycleTest { | |
Vertx vertx; | |
@BeforeEach | |
void prepare() { | |
vertx = Vertx.vertx(new VertxOptions() | |
.setMaxEventLoopExecuteTime(1000) | |
.setPreferNativeTransport(true) | |
.setFileResolverCachingEnabled(true)); | |
} | |
@Test | |
@DisplayName("⬆️ Deploy SampleVerticle") | |
void deploySampleVerticle(VertxTestContext testContext) { | |
vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> testContext.completeNow())); | |
} | |
@Test | |
@DisplayName("🛂 Make a HTTP client request to SampleVerticle") | |
void httpRequest(VertxTestContext testContext) { | |
WebClient webClient = WebClient.create(vertx); | |
vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> { | |
webClient.get(11981, "localhost", "/yo") | |
.as(BodyCodec.string()) | |
.send(testContext.succeeding(resp -> { | |
testContext.verify(() -> { | |
assertThat(resp.statusCode()).isEqualTo(200); | |
assertThat(resp.body()).contains("Yo!"); | |
testContext.completeNow(); | |
}); | |
})); | |
})); | |
} | |
@AfterEach | |
void cleanup() { | |
vertx.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment