Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a custom VerificationMode classes for Mockito
    primarykey
    data
    text
    <p>I would like to verify some things using Mockito but studying the documentation has lead me to believe that it's not possible without going outside the usual Mockito tools. Take this for an example:</p> <pre><code>DrawTool tool = mock(DrawTool.class); new Drawer().draw(tool); verify(tool).begin(); // Make sure begin and end are called exactly once each verify(tool).end(); InOrder inOrder = inOrder(tool); inOrder.verify(tool).begin(); inOrder.verify(tool).end(); inOrder.verify(tool).flush(); inOrder.verifyNoMoreInteractions(); </code></pre> <p>This test verifies several things well, such as verifying that <code>flush</code> is the last interaction, but it seems that Mockito has no way to verify that <code>begin</code> is the first interaction. I'm surprised by that asymmetry in Mockito's tools and so I'm investigating the possibility of creating custom VerificationModes. I'd like to create a VerificationMode called something like <code>beforeAnyOther</code> and use it like this:</p> <pre><code>inOrder.verify(tool, beforeAnyOther()).begin(); inOrder.verify(tool).end(); inOrder.verify(tool, beforeAnyOther()).flush(); inOrder.verifyNoMoreInteractions(); </code></pre> <p>The intent would be to verify that <code>begin</code> is called first and that there are no relevant interactions between <code>end</code> and <code>flush</code> while leaving the interactions between <code>begin</code> and <code>end</code> unspecified.</p> <p>I have been studying the source code for the existing VerificationModes and it seems that in principle this should be a simple VerificationMode to implement, but once I get beyond a few of Mockito's major classes the documentation becomes very thin, almost like it's trying to tell me that I shouldn't be touching these classes. I'm especially wary of the packages that start <code>org.mockito.internal</code> because a name like that suggests to me that these classes are subject to change even though they are public.</p> <p>The really important classes for implementing a VerificationMode seem to be all in the <code>org.mockito.internal.verification.api</code> package. That entire package seems to have only one bit of javadoc, and it says, "This package should be open to public once verification API is fully finished." Does that mean that this package is being actively modified and so I shouldn't use anything it contains, or is that just something that it has said for years and the package will probably never actually change?</p> <p>If I can't use the classes in <code>org.mockito.internal.verification.api</code> then it seems it is impossible to implement custom VerificationModes. Is there a way to do things like this without custom VerificationModes?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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