### Question:
BlockchainEvents { public ListenerRegistration addBalanceListener(@NonNull final EventListener<Balance> listener) { checkNotNull(listener, "listener"); ServerSentEvent serverSentEvent = server .transactions() .forAccount(accountKeyPair) .cursor(CURSOR_FUTURE_ONLY) .stream(new org.stellar.sdk.requests.EventListener<TransactionResponse>() { @Override public void onEvent(TransactionResponse transactionResponse) { extractBalanceChangeFromTransaction(transactionResponse, listener); } }); return new ListenerRegistration(serverSentEvent); } BlockchainEvents(Server server, String accountId, KinAsset kinAsset); ListenerRegistration addBalanceListener(@NonNull final EventListener<Balance> listener); ListenerRegistration addPaymentListener(@NonNull final EventListener<PaymentInfo> listener); ListenerRegistration addAccountCreationListener(final EventListener<Void> listener);  }### Answer:
@Test public void addBalanceListener() throws Exception { enqueueTransactionsResponses(); final CountDownLatch latch = new CountDownLatch(1); final List<Balance> actualResults = new ArrayList<>(); blockchainEvents.addBalanceListener(new EventListener<Balance>() { @Override public void onEvent(Balance data) { actualResults.add(data); if (actualResults.size() == 2) { latch.countDown(); } } }); latch.await(1, TimeUnit.SECONDS); assertThat(actualResults.size(), equalTo(2)); Balance balance1 = actualResults.get(0); Balance balance2 = actualResults.get(1); assertThat(balance1, notNullValue()); assertThat(balance2, notNullValue()); assertThat(balance1.value(), equalTo(new BigDecimal("5387.216"))); assertThat(balance2.value(), equalTo(new BigDecimal("5239.89036"))); }
@Test public void addBalanceListener_StopListener_NoEvents() throws Exception { final int[] eventsCount = {0}; ListenerRegistration listenerRegistration = blockchainEvents .addBalanceListener(new EventListener<Balance>() { @Override public void onEvent(Balance data) { eventsCount[0]++; } }); listenerRegistration.remove(); enqueueCreateAccountResponses(); Thread.sleep(500); assertThat(eventsCount[0], equalTo(0)); }
@SuppressWarnings("ConstantConditions") @Test public void addBalanceListener_NullListener_IllegalArgumentException() throws Exception { expectedEx.expect(IllegalArgumentException.class); expectedEx.expectMessage("listener"); blockchainEvents.addBalanceListener(null); }