Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do it the same way you do it with Mockito on real instances. For example you can chain stubs, the following line will make the first call do nothing, then second and future call to <code>getResources</code> will throw the exception :</p> <pre class="lang-java prettyprint-override"><code>// 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 </code></pre> <p>Thanks to a remark of Matt Lachman, note that if the default answer is not changed at mock creation time, the mock will do nothing by default. Hence writing the following code is equivalent to not writing it.</p> <pre class="lang-java prettyprint-override"><code>doNothing().doThrow(Exception.class).when(StaticResource.class); StaticResource.getResource("string"); </code></pre> <p>Though that being said, it can be interesting for colleagues that will read the test that you expect nothing for this particular code. Of course this can be adapted depending on how is perceived <em>understandability</em> of the test.</p> <hr> <p>By the way, in my humble opinion you should avoid mocking static code if your crafting new code. At Mockito we think it's usually a hint to bad design, it might lead to poorly maintainable code. Though existing legacy code is yet another story.</p> <p>Generally speaking if you need to mock private or static method, then this method does too much and should be externalized in an object that will be injected in the tested object.</p> <p>Hope that helps.</p> <p>Regards</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload