Stubbing and Verification
Stubbing and Verification
introduce:
When using the unit testing framework, the behavior of the simulation object is very important for the writing and execution of the test code.Mockito Junit Jupiter is a popular Java test framework that provides a simple and powerful way to simulate the object.This article will focus on the concepts of Stubbing and Verification in the Mockito Junit Jupiter framework, and provide the corresponding Java code examples.
Stubbing (pile)
In the test code, Stubbing refers to the behavior of the simulation object, providing a expected return value for a specific method call.Stubbing allows us to define the behavior of objects in testing in order to better control the test environment.
The following is an example that shows how to use Mockito to perform stubbing in Junit Jupiter:
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
class ExampleClassTest {
@Mock
private ExampleClass exampleClass;
@Test
void testStubbing() {
MockitoAnnotations.openMocks(this);
// For a specific method call, define the return value that needs to be simulated
when(exampleClass.methodToStub()).thenReturn("mocked value");
// Is the value returned to whether the value returned meets expectations
assertEquals("mocked value", exampleClass.methodToStub());
}
}
In this example, the ExampleClass class is injected into the test class by using the @Mock annotation.Then, initialize the simulation object by calling the method of `MockitoanNotations.openmocks`.Next, use the simulation behavior of the `MOTHODTOSTUB" method of the `MECKED VALUE" with the simulation behavior of the `Mocked Value").Finally, by calling the `Assertequals` to verify whether the simulation return value meets the expectations.With Stubbing, we can control the behavior of the test object in the test to better test.
VERIFICATION
Verification refers to the process of checking the method of the simulation object in the test code.By verifying, we can ensure that the key method calls have occurred, so that the code is correctly called the method of the test object when expected.
The following is an example that shows how to use Mockito to perform Verification in Junit Jupiter:
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
class ExampleClassTest {
@Mock
private ExampleClass exampleClass;
@Test
void testVerification() {
MockitoAnnotations.openMocks(this);
// Call the method of being tested
exampleClass.methodToVerify();
// Verify whether the method is called
verify(exampleClass).methodToVerify();
}
}
In this example, we use the same annotation and initialization method as Stubbing to define the simulation class of the test object.Then, we called the method of `ExampleClass.Methodtoverify (), which is defined in the tested class.Next, verify that the method is called by calling the method by calling the method by calling the `Verify (ExampleClass) .methodtoverify ()` `` `).The verification method called this method is very common in Mockito and can be used to ensure the calls of key methods.
Summarize:
The Mockito Junit Jupiter framework provides powerful functions to simulate the behavior of the object and verify the call.Stubbing allows us to define the behavior of the object in the test, and Verification allows us to verify the call.Using Mockito, we can better control the test environment and verify whether the code is called according to the expected manner.These mechanisms are very important for writing high -quality test code and can help us ensure the correctness of the code.
This is the basic concepts and instances of Stubbing and Verification in the Mockito Junit Jupiter framework.By using these powerful functions, we can better write and perform unit testing, thereby improving our code quality and reliability.