Created
November 3, 2022 12:43
-
-
Save Stwissel/06e0ee2ded07460cce80248435fef5e5 to your computer and use it in GitHub Desktop.
Sample JUnit5 test using the vert.x test extension to run asynchronous tests
This file contains hidden or 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
package com.notessensei.demo; | |
import static org.junit.jupiter.api.Assertions.*; | |
import java.util.stream.Stream; | |
import com.hcl.tests.UnitTest; | |
import org.junit.jupiter.api.AfterEach; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.params.ParameterizedTest; | |
import org.junit.jupiter.params.provider.Arguments; | |
import org.junit.jupiter.params.provider.MethodSource; | |
import io.vertx.core.Vertx; | |
import io.vertx.core.http.HttpServer; | |
import io.vertx.core.json.JsonObject; | |
import io.vertx.ext.web.Router; | |
import io.vertx.ext.web.RoutingContext; | |
import io.vertx.ext.web.client.WebClient; | |
import io.vertx.ext.web.handler.BodyHandler; | |
import io.vertx.junit5.VertxTestContext; | |
@ExtendWith(VertxExtension.class) | |
class HttpServerTest { | |
static final int thePort = 8042; | |
HttpServer server; | |
@BeforeEach | |
void beforeEach(final Vertx vertx, final VertxTestContext testContext) throws Exception { | |
final Router router = Router.router(vertx); | |
router.route().handler(BodyHandler.create()); | |
router.route().handler(this::echo); | |
vertx.createHttpServer() | |
.requestHandler(router) | |
.listen(thePort) | |
.onFailure(testContext::failNow) | |
.onSuccess(h -> { | |
this.server = h; | |
testContext.completeNow(); | |
}); | |
} | |
@AfterEach | |
void afterEach(final Vertx vertx, final VertxTestContext testContext) throws Exception { | |
this.server.close() | |
.onFailure(testContext::failNow) | |
.onSuccess(v -> testContext.completeNow()); | |
} | |
@ParameterizedTest | |
@MethodSource("testCases") | |
void test(final String key, final String value, final Vertx vertx, | |
final VertxTestContext testContext) { | |
final WebClient client = WebClient.create(vertx); | |
final JsonObject body = new JsonObject().put(key, value); | |
client.post(thePort, "localhost", "/") | |
.putHeader("ContentType", "application/json") | |
.sendJson(body) | |
.onFailure(testContext::failNow) | |
.onSuccess(result -> { | |
testContext.verify(() -> { | |
Assertions.assertEquals(200, result.statusCode()); | |
Assertions.assertEquals(body, result.bodyAsJsonObject()); | |
}); | |
testContext.completeNow(); | |
}); | |
} | |
@ParameterizedTest | |
@MethodSource("testCases") | |
void test2(final String key, final String value, final Vertx vertx, | |
final VertxTestContext testContext) { | |
final WebClient client = WebClient.create(vertx); | |
final JsonObject body = new JsonObject().put(key, value); | |
client.post(thePort, "localhost", "/") | |
.putHeader("ContentType", "application/json") | |
.sendJson(body) | |
.onFailure(testContext::failNow) | |
.onSuccess(result -> { | |
testContext.verify(() -> { | |
Assertions.assertEquals(200, result.statusCode()); | |
Assertions.assertEquals(body, result.bodyAsJsonObject()); | |
}); | |
testContext.completeNow(); | |
}); | |
} | |
/** | |
* Replies with a delay | |
* | |
* @param ctx RoutingContext | |
*/ | |
void echo(final RoutingContext ctx) { | |
final JsonObject body = ctx.body().asJsonObject(); | |
ctx.vertx().setTimer(1000, h -> { | |
ctx.response().putHeader("ContentType", "application/json"); | |
ctx.response().end(body.toBuffer()); | |
}); | |
} | |
static Stream<Arguments> testCases() { | |
return Stream.of( | |
Arguments.of("color", "red"), | |
Arguments.of("dance", "tango"), | |
Arguments.of("food", "noodles"), | |
Arguments.of("sky", "blue"), | |
Arguments.of("happy", "ness"), | |
Arguments.of("dance", "tango"), | |
Arguments.of("food", "noodles"), | |
Arguments.of("sky", "blue"), | |
Arguments.of("happy", "ness"), | |
Arguments.of("dance", "tango"), | |
Arguments.of("food", "noodles"), | |
Arguments.of("sky", "blue"), | |
Arguments.of("happy", "ness")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment