如何编写模拟方法以使用Web3j Api获得以太平衡。我正在尝试模拟Web3j和Callig EthGetBalance方法,但我得到的是Null

时间:2018-11-25 02:09:14

标签: java unit-testing mockito ethereum

此方法使用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);
}

0 个答案:

没有答案