Mockito Groovy Support framework for best practice guidelines
Mockito is a popular Java test framework that can simulate virtual objects for simplifying unit testing.The Mockito framework is mainly based on Java language, but also provides support for Groovy language.This article will provide you with the best practical guide to Mockito Groovy Support framework, and provide Java code example for the situation.
1. Installation and setting
1. The dependencies of introducing the Mockito library in the project:
groovy
dependencies {
testCompile 'org.mockito:mockito-core:3.11.0'
}
2. Import the required Mockito and Groovy related classes in the test class:
groovy
import static org.mockito.Mockito.*
import groovy.mock.interceptor.*
import org.mockito.Mock
import org.mockito.InjectMocks
2. Create a virtual object
Mockito Groovy Support creates a virtual object through the `mock` function. The example code is as follows:
groovy
def calculatorMock = mock(Calculator.class)
3. Set the behavior of virtual objects
1. Back to set the virtual object with the method of using the `When-thenreturn` mode to set the virtual object:
groovy
when(calculatorMock.add(2, 3)).thenReturn(5)
2. Use the method of setting the virtual object with the `Doreturn-When` mode to set the virtual object:
groovy
doReturn(5).when(calculatorMock).add(2, 3)
4. Verification method call
1. Use the `Verify` method to verify whether the method is called:
groovy
verify(calculatorMock).add(2, 3)
2. Use the number of times to verify the method of verification method of the `Verify (Times)` method:
groovy
verify(calculatorMock, times(2)).add(2, 3)
5. Capture method parameters
1. Use the parameters of the capture method of `argumentcaptor` to further verify:
groovy
def argCaptor = ArgumentCaptor.forClass(Integer)
verify(calculatorMock).add(argCaptor.capture(), argCaptor.capture())
6. Reset the virtual object
Use the `Reset` method to reset the virtual object to maintain independence between each test case:
groovy
reset(calculatorMock)
7. Use @Injectmocks Comment to automatically inject virtual objects
Using `@injectmocks` Note can automatically inject the virtual object into the tested object. The example code is as follows:
class CalculatorService {
@InjectMocks
private Calculator calculatorMock
// ...
}
8. Set the default behavior of virtual objects
Use the @mockitosettings `annotation can set the default behavior for the virtual object. The example code is as follows:
groovy
@MockitoSettings
class CalculatorTest {
@Mock(defaultAnswer = Answers.RETURNS_SMART_NULLS)
Calculator calculatorMock
// ...
}
Nine, combined with Groovy characteristics to make more advanced simulation
1. Simulate according to the method parameter:
groovy
when(calculatorMock.add(_) >> { int a -> a * 2 })
2. Simulate according to the method parameters and calls:
groovy
when(calculatorMock.add(debug: true)(_)).thenAnswer({ int a -> a * 2 })
The above is the best practical guide to the Mockito Groovy Support framework. By using these guides, you can better use Mockito in the Groovy environment for unit testing.Hope this article will help you!