@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(); }
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 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 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); }
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); }
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);  }