This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Powermock static method is not getting executed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I want to test that a file is getting created by the following method from KClient calss:


my JUnit test is


however no file is being created in expected location and also in Unit log the following message is displayed:


Wanted but not invoked java.net.HttpURLConnection.getInputStream();
Actually, there were zero interactions with this mock.



What can be the reason for such a behaviour.

Regards,
 
Tim Werner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have managed to solve the issue however I don't know how to mock a local variable like String folderLocation = propsDocu.getProperty(OUTPUT_FOLDER);
 
Tim Werner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried to mock the File creation in order to replace it with a dummy file.

However I'm getting NullPointerException.
 
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are again creating mocks where you shouldn't be. You say you want to test the static method createImageFileBasedOnStream() method and yet you have mockStatic(KVSClient.class). This means that the call you make on line 20 is done on a mock object. You don't seem to understand how completely wrong this is, and pointless. You do NOT test mock objects.

Here's the biggest trap you're falling into. Somehow you've convinced yourself that your test is at least partially "working" and therefore valid. It is, however, invalid because you're not exercising any real code. You're only exercising mock objects! That is, you're creating mock objects, telling them to react in certain ways, then you're invoking the mocks, and then verifying that they actually did what you told them to do in the first place! Of course it's going to look like your test "passed"!

Line 16 in your test says "Ok, PowerMock, mock out any static methods of the KVSClient class." PowerMock dutifully mocks (creates a "fake" object that looks like) KVSClient and intercepts any calls to KVSClient static methods. That means that a call to KVSClient.createImageFileBasedOnStream() will no longer go to the actual code you say want to test but will get rerouted to this fake object that looks like a KVSClient.

Line 20 makes a call to that fake version of KVSClient.createImageFileBasedOnStream() method.  Again, this is not the code you said wanted to test that is executed. This is a MOCK implementation that's being called (because you made this happen by writing line 16)

Line 24 you tell PowerMokito to verify that the static method on KVSClient.class (which you have mocked up with line 16) was called. Essentially, you're saying, "Hey, PowerMock, verify that I actually made the call that I just made on line 20."

Do you see how silly and useless this test is now?
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to see proof for yourself, put a breakpoint on any line of the static createImageFileBasedOnStream() method and run your test in debug mode. Execution will never go into the real code and that breakpoint will never be activated unless you delete or comment out line 16.
 
knowledge is the difference between drudgery and strategic action -- tiny ad
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!