### Question:
EthereumWalletService { public BigInteger estimateGasLimit(String fromAddress, String toAddress, Function function, BigInteger weiValue) { try { EthEstimateGas ethEstimateGas = web3j.ethEstimateGas(new Transaction(fromAddress, null, null, null , toAddress, weiValue, FunctionEncoder.encode(function))).send(); if (ethEstimateGas.getError() != null) { String parameters = function.getInputParameters().stream().map(TypeEncoder::encode).collect(Collectors.joining(",")); userErrorMessageDao.add(new UserErrorMessage(null, fromAddress, ethEstimateGas.getError().getMessage(), function.getName(), parameters, System.currentTimeMillis())); throw new NotValidTransactionException(String.format("Unable to send eth transaction from %s to %s : %s, error - %s", fromAddress, toAddress, function.getName(), ethEstimateGas.getError().getMessage())); } BigInteger amountUsed = ethEstimateGas.getAmountUsed(); return amountUsed.add(amountUsed.divide(BigInteger.TEN)); } catch (IOException e) { throw new RuntimeException("I/O error occurred, maybe eth node is down"); } } @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 testEstimateGasWithError() throws IOException { mockEstimateGasRequestResponse(new Response.Error(1, "Error"), null); assertThrows(NotValidTransactionException.class, () -> service.estimateGasLimit(accountAddress, "", function, BigInteger.TEN)); }
@Test void testEstimateGas() throws IOException { mockEstimateGasRequestResponse(null, BigInteger.TEN); BigInteger gasLimit = service.estimateGasLimit(accountAddress, "", function, BigInteger.TEN); assertEquals(BigInteger.valueOf(11), gasLimit); }
@Test void testEstimateGasWithException() throws IOException { Request request = mockEstimateGas(); doThrow(new IOException()).when(request).send(); assertThrows(RuntimeException.class, () -> service.estimateGasLimit(accountAddress, "", function, BigInteger.TEN)); }