1. Common annotations
@PrepareForTest and @RunWith appear in pairs. Generally, the values of @RunWith() and @PrepareForTest are the referenced static method or private method class.
The class annotated by @InjectMocks will be injected into all classes annotated by @Mock annotated.
@Before annotation is used for methods, indicating that they are executed before executing the method annotated by @Test annotation.
In the initMocks() method, you can execute (this); to inject the @Mock annotation modified class into the @InjectMocks modified class.
2. Example operation
First, we define three classes, namely entity class, data operation class, and business logic class. The following are the specific definitions of the three classes:
public class Employee { private String name; private Double salary; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { = name; } /** * @return the salary */ public Double getSalary() { return salary; } /** * @param salary the salary to set */ public void setSalary(Double salary) { = salary; } }
:
public interface EmployeeDao { public Employee getEmployee(String name); public boolean updateEmployee(Employee employee); public static void deleteEmployee(String name) { throw new NullPointerException(); } public Exception saveEmployee(Employee employee); }
:
public class EmployeeServiceImpl { private EmployeeDao employeeDao; public EmployeeServiceImpl() { } public EmployeeServiceImpl(EmployeeDao employeeDao) { = employeeDao; } public Employee getEmployee(String name) { return (name); } public boolean updateEmployee(Employee employee) { return (employee); } public boolean deleteEmployee(String name) { try { (name); return true; } catch (Exception e) { return false; } } public boolean saveEmployee(Employee employee) { try { (employee); return true; } catch (Exception e) { (); return false; } } }
PowerMockito knowledge points
Here are the PowerMockito knowledge points involved in the test class:
1) This involves static methods, so you need to use @RunWith()
@PrepareForTest({ }) annotation, the value of PrepareForTest is the class or interface where the static method is located, otherwise the following error will be reported:
: The class not prepared for test. To prepare this class, add class to the '@PrepareForTest' annotation. In case if you don't use this annotation, add the annotation on class or method level. at (:32)
2) Use the @Before annotation to initialize the method of the @Test annotation, such as using mock (class object) to create an object that needs to be mocked, so that we can assume that the mocked object has been implemented, although it is actually an interface that does not implement the class.
3) After the mock object is created, we can use when (called the method of mock object).thenReturn (defines the return value of the method call), so we are equivalent to assuming that the value defined in thenReturn after calling the method of the mock object is always returned. In this way, we don’t have to care about the implementation of the simulated object, but only care about whether there are problems with the business logic we tested below.
@RunWith() @PrepareForTest({ }) public class EmployeeServiceImplTest { @Mock public EmployeeDao employeeDao; @Mock EmployeeServiceImpl employeeServiceImpl; // @Before annotated method will perform initialization action before calling the test method @Before public void initMocks() { // Create an instance of the mock object EmployeeDao employeeDao = mock(); // Assign the simulated object to the business instance employeeServiceImpl = new EmployeeServiceImpl(employeeDao); } @Test public void getEmployeeTest() { String name = "scott"; Employee employee = new Employee(); ("scott"); (8888.0); // Define the employee object always returns when executing the (name) method, which is equivalent to implementing the employeeDao method when((name)).thenReturn(employee); /* * The following tests the (name) method we want to unit test. We have blocked the call to (name) of this method, which is equivalent to disconnecting the dependency. * In this way, we only need to care whether there is a problem with the logic of the (name) method */ Employee employee2 = (name); (()); } @Test public void updateEmployeeTest() { Employee employee = new Employee(); ("tiger"); (99999.0); when((anyObject())).thenReturn(true); Employee employee2 = new Employee(); ("scott"); (99999.0); Boolean boolean1 = (employee2); (boolean1); } @Test public void deleteEmployeeTest() { String name = "haha"; // Because the static method is called here, use(); to simulate static classes (); // Use doNothing() to define doing nothing when executing the following statement ().when(); // This sentence will not throw the exception even if the doNothing() above (name); // Therefore, no exception was found to return true when executing (name) EmployeeServiceImpl employeeServiceImpl = new EmployeeServiceImpl(); ((name)); } @Test public void throwDeleteExceptionTest() { String name = "haha"; (); // doThrow() defines the following sentence and throws an exception (new NullPointerException()).when(); (name); // Therefore (name) execution will return false EmployeeServiceImpl employeeServiceImpl = new EmployeeServiceImpl(); assertTrue((name)); } @Test public void saveEmployeeTest() { Employee employee = new Employee(); ("scott"); (8888.0); // Piling, define the value returned by the method when((employee)).thenReturn(new NullPointerException()); // doNothing() does not take effect here because the instance method is called here, not a static method ().when(employeeDao).saveEmployee(employee); (employee); } @Test public void throwSaveEmployeeTest() { Employee employee = new Employee(); ("scott"); (8888.0); when((employee)).thenReturn(new NullPointerException()); // doThrow() here actually does not take effect because the instance method is called here, not a static method, so there is no effect of causing the next statement to throw exceptions (new NullPointerException()).when(employeeDao).saveEmployee(employee); (employee); } }
This is the end of this article about PowerMock usage practice in Java. For more related content on PowerMock usage, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!