• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

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: 15989
265
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: 15989
265
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.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic