此方法使用web3j Api提取以太。
public static BigInteger OriginalMethod(){
// connect to node
Web3j web3 = Web3j.build(new HttpService()); // defaults to
http://localhost:8545/
// send asynchronous requests to get balance
EthGetBalance ethGetBalance = web3
.ethGetBalance("0xAccountAddress", DefaultBlockParameterName.LATEST)
.sendAsync()
.get();
BigInteger wei = ethGetBalance.getBalance();
}
以下方法是获取以太币的模拟方法。
public static BigInteger MockOriginalMethod(){
web3j=Mockito.mock(Web3j.class);//Mock web3j class
BigInteger bigValue=Mockito.mock(BigInteger.class);
Mockito.when(web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).sendAsync()
.get().getBalance()).thenReturn(bigValue);
return bigValue.valueOf(11);
}