How do I make fun of the static method that void returns using PowerMock?

I have several static usage methods in my project, some of them just pass or throw an exception. There are many examples of how to mock a static method that has a return type other than void. But how can I mock a static method that returns void only " doNothing() "?

The non-void version uses the following lines of code:

 @PrepareForTest(StaticResource.class) 

...

 PowerMockito.mockStatic(StaticResource.class); 

...

 Mockito.when(StaticResource.getResource("string")).thenReturn("string"); 

However, if applied to StaticResources , which returns void , the compiler will complain that when(T) not applicable for void ...

Any ideas?

The workaround would probably be to have all static methods return some Boolean for success, but I don't like the workarounds.

+42
static mockito powermock mocking void
Mar 06 2018-12-12T00:
source share
4 answers

You can do it the same way you do it with Mockito on real instances. For example, you can chain stubs, the next line will make the first call do nothing, then the second and future call getResources will throw an exception:

 // the stub of the static method doNothing().doThrow(Exception.class).when(StaticResource.class); StaticResource.getResource("string"); // the use of the mocked static code StaticResource.getResource("string"); // do nothing StaticResource.getResource("string"); // throw Exception 

Thanks to Matt Lachman’s remark, note that if the default answer does not change when the layout is created, the layout will not do anything by default. Therefore, writing the following code is equivalent to not writing it.

 doNothing().doThrow(Exception.class).when(StaticResource.class); StaticResource.getResource("string"); 

Although this, as they say, may be interesting for colleagues who read a test that you do not expect anything for this particular code. Of course, this can be adapted depending on how the comprehensibility of the test is perceived.




By the way, in my humble opinion, you should avoid mocking static code if you are creating new code. At Mockito, we think this is usually a hint of poor design, which can lead to poor code maintenance. Although existing legacy code is another story.

Generally speaking, if you need to mock a private or static method, then this method is too much and must be externalized in the object that will be introduced into the test object.

Hope this helps.

Hello

+22
Mar 06 '12 at 14:30
source share

You can drown out the static void method as follows:

 PowerMockito.doNothing().when(StaticResource.class, "getResource", anyString()); 

Although I'm not sure why you will worry, because when you call mockStatic (StaticResource.class) , all static methods in StaticResource are truncated by default

More useful, you can write the value passed to StaticResource.getResource () like this:

 ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); PowerMockito.doNothing().when( StaticResource.class, "getResource", captor.capture()); 

You can then evaluate the String that was passed to StaticResource.getResource as follows:

 String resourceName = captor.getValue(); 
+52
Jul 02 '13 at 8:10
source share

Simply put, Imagine if you want to poke fun at a line:

 StaticClass.method(); 

then you write below a line of code to make fun of:

 PowerMockito.mockStatic(StaticClass.class); PowerMockito.doNothing().when(StaticClass.class); StaticClass.method(); 
+5
May 04 '15 at 12:30
source share

To make fun of a static method that returns void, for example. Fileutils.forceMKdir(File file),

Code example:

 File file =PowerMockito.mock(File.class); PowerMockito.doNothing().when(FileUtils.class,"forceMkdir",file); 
+1
Jul 07 '16 at 8:09
source share



All Articles