### Question:
PortfolioServiceImpl implements PortfolioService { @Override public void buy(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot buy " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } if (quote.getInteger("shares") < amount) { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " - not enough " + "stocks on the market (" + quote.getInteger("shares") + ")")); return; } double price = amount * quote.getDouble("ask"); String name = quote.getString("name"); if (portfolio.getCash() >= price) { portfolio.setCash(portfolio.getCash() - price); int current = portfolio.getAmount(name); int newAmount = current + amount; portfolio.getShares().put(name, newAmount); sendActionOnTheEventBus("BUY", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " of " + name + " - " + "not enough money, " + "need " + price + ", has " + portfolio.getCash())); } } PortfolioServiceImpl(Vertx vertx, ServiceDiscovery discovery, double initialCash); @Override void getPortfolio(Handler<AsyncResult<Portfolio>> resultHandler); @Override void evaluate(Handler<AsyncResult<Double>> resultHandler); @Override void buy(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler); @Override void sell(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler);  }### Answer:
@Test public void testBuyStocks(TestContext tc) { Async async = tc.async(); service.buy(10, quote("A", 10, 20, 100), ar -> { tc.assertTrue(ar.succeeded()); Portfolio portfolio = ar.result(); tc.assertEquals(portfolio.getAmount("A"), 10); tc.assertEquals(portfolio.getAmount("B"), 0); tc.assertEquals(portfolio.getCash(), original.getCash() - 10 * 10); async.complete(); }); }
@Test public void testThatYouCannotBuyIfYouRunOutOfMoney(TestContext tc) { Async async = tc.async(); service.buy(10000, quote("A", 10, 20, 100000), ar -> { tc.assertTrue(ar.failed()); tc.assertTrue(ar.cause().getMessage().contains("not enough money")); async.complete(); }); }
@Test public void testThatYouCannotBuyIfThereIsNotEnoughShare(TestContext tc) { Async async = tc.async(); service.buy(100, quote("A", 10, 20, 10), ar -> { tc.assertTrue(ar.failed()); tc.assertTrue(ar.cause().getMessage().contains("not enough stocks")); async.complete(); }); }
@Test public void testYouCannotBuyANegativeAmount(TestContext tc) { Async async = tc.async(); service.buy(-1, quote("A", 10, 20, 100), ar -> { tc.assertTrue(ar.failed()); async.complete(); }); }