### Question:
ApiVerticle extends AbstractVerticle { private void getProduct(RoutingContext rc) { String itemId = rc.request().getParam("itemid"); catalogService.getProduct(itemId, ar -> { if (ar.succeeded()) { Product product = ar.result(); if (product != null) { rc.response() .putHeader("Content-type", "application/json") .end(product.toJson().encodePrettily()); } } else { rc.fail(ar.cause()); } }); } ApiVerticle(CatalogService catalogService); @Override void start(Future<Void> startFuture);  }### Answer:
@Test public void testGetProduct(TestContext context) throws Exception { String itemId = "111111"; JsonObject json = new JsonObject() .put("itemId", itemId) .put("name", "productName1") .put("desc", "productDescription1") .put("price", new Double(100.0)); Product product = new Product(json); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation){ Handler<AsyncResult<Product>> handler = invocation.getArgument(1); handler.handle(Future.succeededFuture(product)); return null; } }).when(catalogService).getProduct(eq("111111"),any()); Async async = context.async(); vertx.createHttpClient().get(port, "localhost", "/product/111111", response -> { assertThat(response.statusCode(), equalTo(200)); assertThat(response.headers().get("Content-type"), equalTo("application/json")); response.bodyHandler(body -> { JsonObject result = body.toJsonObject(); assertThat(result, notNullValue()); assertThat(result.containsKey("itemId"), is(true)); assertThat(result.getString("itemId"), equalTo("111111")); verify(catalogService).getProduct(eq("111111"),any()); async.complete(); }) .exceptionHandler(context.exceptionHandler()); }) .exceptionHandler(context.exceptionHandler()) .end(); }
@Test public void testGetNonExistingProduct(TestContext context) throws Exception { doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation){ Handler<AsyncResult<Product>> handler = invocation.getArgument(1); handler.handle(Future.succeededFuture(null)); return null; } }).when(catalogService).getProduct(eq("111111"),any()); Async async = context.async(); vertx.createHttpClient().get(port, "localhost", "/product/111111", response -> { assertThat(response.statusCode(), equalTo(404)); async.complete(); }) .exceptionHandler(context.exceptionHandler()) .end(); }
@Test public void testGetProductWhenCatalogServiceThrowsError(TestContext context) { doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation){ Handler<AsyncResult<List<Product>>> handler = invocation.getArgument(1); handler.handle(Future.failedFuture("error")); return null; } }).when(catalogService).getProduct(any(),any()); Async async = context.async(); vertx.createHttpClient().get(port, "localhost", "/product/111111", response -> { assertThat(response.statusCode(), equalTo(503)); response.bodyHandler(body -> { assertThat(body.toString(), equalTo("Service Unavailable")); verify(catalogService).getProduct(eq("111111"),any()); async.complete(); }) .exceptionHandler(context.exceptionHandler()); }) .exceptionHandler(context.exceptionHandler()) .end(); }