How can I determine if an object is mokkit?

Is it possible to indicate in the code if this object is a Mockito mock or not?

The reason I would like to do this is to return another error message when it is used. This will be used to invite other developers to use a pre-prepared layout that is already set up to answer calls in a useful way, rather than creating the layout itself.

At the moment, the best I have is object.getClass().getName().contains("EnhancerByMockitoWithCGLIB") , but it seems to be hacked.

+46
java mockito
Jan 27 '12 at 15:36
source share
2 answers

It seems that there is no such API (please raise a question, it should be!) Fortunately (after your comment below) the org.mockito.internal.util package has a method:

 import org.mockito.internal.util.MockUtil; new MockUtil().isMock(obj) 

In the future, the Mockito.isMock() method may be added to the public API, see Issue 313: Provide isMock outside of org.mockito.internal) .

+46
Jan 27 2018-12-12T00:
source share
β€” -

As a continuation, the open Mockito API now has this method:

 MockingDetails org.mockito.Mockito.mockingDetails(Object toInspect) 

This is the result of the problem that @David Wallace raised. The returned object supports the methods 'isMock ()' as well as 'isSpy ()', and can later be improved to provide additional information related to mock.

+31
Feb 28 '13 at 14:56
source share



All Articles