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:
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
Brice Mar 06 '12 at 14:30 2012-03-06 14:30
source share