@Test public void testAddProduct(TestContext context) throws Exception { doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation){ Handler<AsyncResult<String>> handler = invocation.getArgument(1); handler.handle(Future.succeededFuture(null)); return null; } }).when(catalogService).addProduct(any(),any()); Async async = context.async(); String itemId = "111111"; JsonObject json = new JsonObject() .put("itemId", itemId) .put("name", "productName") .put("desc", "productDescription") .put("price", new Double(100.0)); String body = json.encodePrettily(); String length = Integer.toString(body.length()); vertx.createHttpClient().post(port, "localhost", "/product") .exceptionHandler(context.exceptionHandler()) .putHeader("Content-type", "application/json") .putHeader("Content-length", length) .handler(response -> { assertThat(response.statusCode(), equalTo(201)); ArgumentCaptor<Product> argument = ArgumentCaptor.forClass(Product.class); verify(catalogService).addProduct(argument.capture(), any()); assertThat(argument.getValue().getItemId(), equalTo(itemId)); async.complete(); }) .write(body) .end(); }
private void addProduct(RoutingContext rc) { JsonObject json = rc.getBodyAsJson(); catalogService.addProduct(new Product(json), ar -> { if (ar.succeeded()) { rc.response().setStatusCode(201).end(); } else { rc.fail(ar.cause()); } }); }
ApiVerticle extends AbstractVerticle { private void addProduct(RoutingContext rc) { JsonObject json = rc.getBodyAsJson(); catalogService.addProduct(new Product(json), ar -> { if (ar.succeeded()) { rc.response().setStatusCode(201).end(); } else { rc.fail(ar.cause()); } }); } }
ApiVerticle extends AbstractVerticle { private void addProduct(RoutingContext rc) { JsonObject json = rc.getBodyAsJson(); catalogService.addProduct(new Product(json), ar -> { if (ar.succeeded()) { rc.response().setStatusCode(201).end(); } else { rc.fail(ar.cause()); } }); } ApiVerticle(CatalogService catalogService); }
ApiVerticle extends AbstractVerticle { private void addProduct(RoutingContext rc) { JsonObject json = rc.getBodyAsJson(); catalogService.addProduct(new Product(json), ar -> { if (ar.succeeded()) { rc.response().setStatusCode(201).end(); } else { rc.fail(ar.cause()); } }); } ApiVerticle(CatalogService catalogService); @Override void start(Future<Void> startFuture); }
ApiVerticle extends AbstractVerticle { private void addProduct(RoutingContext rc) { JsonObject json = rc.getBodyAsJson(); catalogService.addProduct(new Product(json), ar -> { if (ar.succeeded()) { rc.response().setStatusCode(201).end(); } else { rc.fail(ar.cause()); } }); } ApiVerticle(CatalogService catalogService); @Override void start(Future<Void> startFuture);  }