• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Mock static method called from constructor in java class

Resolved: Mock static method called from constructor in java class

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I want to mock the static method being invoked from the constructor of my class.
My class:
Here NodeUtils.getMyNode() is the static method being invoked from the constructor of the class.
NodeUtils.java Class:
MyClassTest.java
When I try & run the only test case I get the following error:
I am not sure how do we mock the method to avoid this error?
Since I am new to Java I am not able to catch this. Any help here is appreciated.

Answer:

So to answer your question how to mock a static method: mockito allows this since version 3.8.0. You can find a tutorial here at Baeldung
This allows generating a statically mocked Object for a concrete context, which you can create within a try block. For your case this would look like the following.
Fixed Unit Test
Mockito dependency
You need mockito with at least version 3.8.0 in your project.
With maven add:
With gradle add:

If you have better answer, please add a comment about this, thank you!

java junit mockito unit-testing
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Sort dataframe columns value by close it to another column

27/03/2023

Resolved: PostgreSQL resample 1 minute OHLCV candle data into 5 minute OHLCV candle data

27/03/2023

Resolved: How do I navigate a table without any easily accessible distinctions?

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.