How to write a mock method for getting ether balance using Web3j Api. I am trying to mock Web3j and callig...












0















This method fetches ether using 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();
}


The below method is the mock method for getting ether.



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);
}









share|improve this question

























  • What do you want to test with this method? Do you want to test OriginalMethod()? Mockito needs help for example from PowerMockito to mock static methods. Both have good wiki with examples.

    – uli
    Nov 25 '18 at 6:02











  • Yes, I want to test OriginalMethod().

    –  Awanish
    Nov 25 '18 at 10:46











  • Whit such design it is not possible to unit test the method. You are creating Web3j object inside method and using it. There is no way to mock it.

    – uli
    Nov 25 '18 at 15:35
















0















This method fetches ether using 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();
}


The below method is the mock method for getting ether.



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);
}









share|improve this question

























  • What do you want to test with this method? Do you want to test OriginalMethod()? Mockito needs help for example from PowerMockito to mock static methods. Both have good wiki with examples.

    – uli
    Nov 25 '18 at 6:02











  • Yes, I want to test OriginalMethod().

    –  Awanish
    Nov 25 '18 at 10:46











  • Whit such design it is not possible to unit test the method. You are creating Web3j object inside method and using it. There is no way to mock it.

    – uli
    Nov 25 '18 at 15:35














0












0








0








This method fetches ether using 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();
}


The below method is the mock method for getting ether.



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);
}









share|improve this question
















This method fetches ether using 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();
}


The below method is the mock method for getting ether.



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);
}






java unit-testing mockito ethereum






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 10:44







Awanish

















asked Nov 25 '18 at 2:09









Awanish Awanish

213




213













  • What do you want to test with this method? Do you want to test OriginalMethod()? Mockito needs help for example from PowerMockito to mock static methods. Both have good wiki with examples.

    – uli
    Nov 25 '18 at 6:02











  • Yes, I want to test OriginalMethod().

    –  Awanish
    Nov 25 '18 at 10:46











  • Whit such design it is not possible to unit test the method. You are creating Web3j object inside method and using it. There is no way to mock it.

    – uli
    Nov 25 '18 at 15:35



















  • What do you want to test with this method? Do you want to test OriginalMethod()? Mockito needs help for example from PowerMockito to mock static methods. Both have good wiki with examples.

    – uli
    Nov 25 '18 at 6:02











  • Yes, I want to test OriginalMethod().

    –  Awanish
    Nov 25 '18 at 10:46











  • Whit such design it is not possible to unit test the method. You are creating Web3j object inside method and using it. There is no way to mock it.

    – uli
    Nov 25 '18 at 15:35

















What do you want to test with this method? Do you want to test OriginalMethod()? Mockito needs help for example from PowerMockito to mock static methods. Both have good wiki with examples.

– uli
Nov 25 '18 at 6:02





What do you want to test with this method? Do you want to test OriginalMethod()? Mockito needs help for example from PowerMockito to mock static methods. Both have good wiki with examples.

– uli
Nov 25 '18 at 6:02













Yes, I want to test OriginalMethod().

–  Awanish
Nov 25 '18 at 10:46





Yes, I want to test OriginalMethod().

–  Awanish
Nov 25 '18 at 10:46













Whit such design it is not possible to unit test the method. You are creating Web3j object inside method and using it. There is no way to mock it.

– uli
Nov 25 '18 at 15:35





Whit such design it is not possible to unit test the method. You are creating Web3j object inside method and using it. There is no way to mock it.

– uli
Nov 25 '18 at 15:35












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464083%2fhow-to-write-a-mock-method-for-getting-ether-balance-using-web3j-api-i-am-tryin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464083%2fhow-to-write-a-mock-method-for-getting-ether-balance-using-web3j-api-i-am-tryin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python