### Question:
EthereumWalletService { public int getNumberOfConfirmations(String txHash) throws RuntimeException { StringValidator.requireNonBlank(txHash); int confirmations = -1; try { EthGetTransactionReceipt txResponse = web3j.ethGetTransactionReceipt(txHash).send(); TransactionReceipt tx = getResultFrom(txResponse); if (tx != null) { BigInteger txBlockNumber = tx.getBlockNumber(); EthBlockNumber blockNumberResponse = web3j.ethBlockNumber().send(); String blockNumber = getResultFrom(blockNumberResponse); if (blockNumber != null) { confirmations = Numeric.decodeQuantity(blockNumber).subtract(txBlockNumber).intValue(); } } } catch (MessageDecodingException e) { log.warn(e.getMessage(), e, "txHash: " + txHash); } catch (IOException e) { throw new RuntimeException(e); } return confirmations; } @Inject  EthereumWalletService(PropertiesHolder propertiesHolder, KeyStoreService keyStoreService, DexEthService dexEthService, UserErrorMessageDao userErrorMessageDao,
                                 DexBeanProducer dexBeanProducer); @PostConstruct void init(); EthWalletBalanceInfo balanceInfo(String address); BigInteger getOnlyEthBalanceWei(String address); BigInteger getEthOrPaxBalanceWei(String address, DexCurrency dexCurrency); int getNumberOfConfirmations(String txHash); BigInteger getPaxBalanceWei(String address); BigInteger getEthBalanceWei(String address); String transfer(String passphrase, long accountId, String fromAddress, String toAddress, BigDecimal amountEth, Long gasPrice, DexCurrency currencies); String sendApproveTransaction(EthWalletKey ethWalletKey, String spenderAddress, BigInteger value); EthBlock.Block getLastBlock(); BigInteger getAllowance(String spenderAddress, String owner, String contractAddress); String transferEth(Credentials credentials, String toAddress, BigInteger amountWei, Long gasPrice); Optional<TransactionReceipt> getReceipt(String transactionHash); BigInteger estimateGasLimit(String fromAddress, String toAddress, Function function, BigInteger weiValue); BigInteger validateBalanceAndReturnGasLimit(String fromAddress, String toAddress, Function function, BigInteger weiValue, BigInteger gasPrice); final String PAX_CONTRACT_ADDRESS; }### Answer:
@Test void testGetNumberOfConfirmationsWhenTxInLastBlock() throws IOException { mockTxInResponse(BigInteger.valueOf(100)); mockBlockNumberInResponse(BigInteger.valueOf(100)); int numberOfConfirmations = service.getNumberOfConfirmations(txHash); assertEquals(0, numberOfConfirmations); }
@Test void testGetNumberOfConfirmationsWhenTxInPrevBlock() throws IOException { mockTxInResponse(BigInteger.valueOf(100)); mockBlockNumberInResponse(BigInteger.valueOf(103)); int numberOfConfirmations = service.getNumberOfConfirmations(txHash); assertEquals(3, numberOfConfirmations); }
@Test void testGetNumberOfConfirmationsWhenIOErrorOccurred() throws IOException { Request request = mockTxReceiptRequest(); doThrow(new IOException()).when(request).send(); assertThrows(RuntimeException.class, () -> service.getNumberOfConfirmations(txHash)); }