### Question:
TxValidatorAccountBalanceValidator implements TxValidatorStep { @Override public boolean validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) { if (isFreeTx) { return true; } if (state == null) { return false; } BigInteger txGasLimit = tx.getGasLimitAsInteger(); Coin maximumPrice = tx.getGasPrice().multiply(txGasLimit); return state.getBalance().compareTo(maximumPrice) >= 0; }  @Override boolean validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx);  }### Answer:
@Test public void validAccountBalance() { Transaction tx1 = Mockito.mock(Transaction.class); Transaction tx2 = Mockito.mock(Transaction.class); Transaction tx3 = Mockito.mock(Transaction.class); AccountState as = Mockito.mock(AccountState.class); Mockito.when(tx1.getGasLimitAsInteger()).thenReturn(BigInteger.valueOf(1)); Mockito.when(tx2.getGasLimitAsInteger()).thenReturn(BigInteger.valueOf(1)); Mockito.when(tx3.getGasLimitAsInteger()).thenReturn(BigInteger.valueOf(2)); Mockito.when(tx1.getGasPrice()).thenReturn(Coin.valueOf(1)); Mockito.when(tx2.getGasPrice()).thenReturn(Coin.valueOf(10000)); Mockito.when(tx3.getGasPrice()).thenReturn(Coin.valueOf(5000)); Mockito.when(as.getBalance()).thenReturn(Coin.valueOf(10000)); TxValidatorAccountBalanceValidator tvabv = new TxValidatorAccountBalanceValidator(); Assert.assertTrue(tvabv.validate(tx1, as, null, null, Long.MAX_VALUE, false)); Assert.assertTrue(tvabv.validate(tx2, as, null, null, Long.MAX_VALUE, false)); Assert.assertTrue(tvabv.validate(tx3, as, null, null, Long.MAX_VALUE, false)); }
@Test public void invalidAccountBalance() { Transaction tx1 = Mockito.mock(Transaction.class); Transaction tx2 = Mockito.mock(Transaction.class); AccountState as = Mockito.mock(AccountState.class); Mockito.when(tx1.getGasLimitAsInteger()).thenReturn(BigInteger.valueOf(1)); Mockito.when(tx2.getGasLimitAsInteger()).thenReturn(BigInteger.valueOf(2)); Mockito.when(tx1.getGasPrice()).thenReturn(Coin.valueOf(20)); Mockito.when(tx2.getGasPrice()).thenReturn(Coin.valueOf(10)); Mockito.when(as.getBalance()).thenReturn(Coin.valueOf(19)); TxValidatorAccountBalanceValidator tvabv = new TxValidatorAccountBalanceValidator(); Assert.assertFalse(tvabv.validate(tx1, as, null, null, Long.MAX_VALUE, false)); Assert.assertFalse(tvabv.validate(tx2, as, null, null, Long.MAX_VALUE, false)); }
@Test public void balanceIsNotValidatedIfFreeTx() { Transaction tx = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ONE.toByteArray(), BigInteger.valueOf(21071).toByteArray(), new ECKey().getAddress(), BigInteger.ZERO.toByteArray(), Hex.decode("0001"), new TestSystemProperties().getBlockchainConfig().getCommonConstants().getChainId()); tx.sign(new ECKey().getPrivKeyBytes()); TxValidatorAccountBalanceValidator tv = new TxValidatorAccountBalanceValidator(); Assert.assertTrue(tv.validate(tx, new AccountState(), BigInteger.ONE, Coin.valueOf(1L), Long.MAX_VALUE, true)); }