Mocking Annotations
Mocking annotations allow fine grained control over what mocking should be preferred when testing.
Using @InTestsMock
@InTestsMock
Perhaps you have a method that Diffblue Cover would ordinarily test using an Integer
but you'd prefer to see it tested using Mockito.mock(..)
. In this case you could annotate the method (or class, or package) to recommend mocking Number
:
Conversely, if Diffblue Cover normally does mock a particular class, and you have a particular location where it shouldn't be then you can forbid it:
Specifying values to return
When mocking a class, it can be useful to specify what each method returns. This can be achieved using the following attributes:
method
The simple name of the method to mock
method = "intValue"
booleanReturnValues
The boolean value (or an array of values) to return
booleanReturnValues = true
or booleanReturnValues = {true,false}
byteReturnValues
The byte value (or an array of values) to return
byteReturnValues = (byte) 1
or byteReturnValues = {(byte) 1,(byte) 0}
charReturnValues
The char value (or an array of values) to return
charReturnValues = 'A'
or charReturnValues = {'A','B'}
intReturnValues
The int value (or an array of values) to return
intReturnValues = 2
or intReturnValues = {2,3}
shortReturnValues
The short value (or an array of values) to return
shortReturnValues = 3
or shortReturnValues = {3,4}
longReturnValues
The long value (or an array of values) to return
longReturnValues = 4L
or longReturnValues = {4L,5L}
floatReturnValues
The float value (or an array of values) to return
floatReturnValues = 5.0f
or floatReturnValues = {5.0f,6.0f}
doubleReturnValues
The double value (or an array of values) to return
doubleReturnValues = 6.0d
or doubleReturnValues = {6.0d,7.0d}
stringReturnValues
The String value (or an array of values) to return
stringReturnValues = "AAA"
or stringReturnValues = {"AAA","BBB"}
For example, to return a value of 5
from the mocked method Number.intValue()
, the following annotation could be used:
Specifying an array of return values
Since a mocked method can only return a single value, why specify an array of values? Cover will try to use the values in the order they are specified. The value chosen is the value that provides the best test. In most cases, however, specifying only a single value will be sufficient for generating a test with good coverage.
Using @InTestsMockConstruction
@InTestsMockConstruction
Perhaps you have a method that Diffblue Cover is unable to test, and you think it could make more progress using Mockito.mockConstruction(Random.class)
. In this case you could annotate the method (or class, or package) to recommend mocking construction of Random
:
Using @InTestsMockStatic
@InTestsMockStatic
Perhaps you have a method that Diffblue Cover is unable to test, and you think it could make more progress using Mockito.mockStatic(UUID.class)
. In this case you could annotate the method (or class, or package) to recommend mocking static methods of UUID
:
Last updated
Was this helpful?