The best practice of the PowerMock framework in the Java library test

PowerMock is an open source framework for the Java class library test. It combines the functions of Mockito and Easymock, which provides a convenient and powerful unit test ability.In the Java class library test, using PowerMock can solve some traditional test tools that cannot be dealt with, such as simulation of static, private and final methods. The following is the best practice in the PowerMock framework in the Java class library test: 1. Introduce PowerMock dependencies: In the pom.xml file of the project, add PowerMock and related Mockito and Junit dependencies.The example code is as follows: <dependencies> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-core</artifactId> <version>2.0.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>2.0.2</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> </dependencies> The 2.0.2 version of PowerMock is used here. The PowerMock-Module-Junit4 is a PowerMock module for Junit 4. PowerMock-API-Mockito2 is a PowerMock module for Mockito 2.x. 2. Use @preparefortest annotation: Add @Preparefortest annotations to the test class that needs to be simulated, and specify a class to mock.This will tell PowerMock for special treatment of these classes in order to simulate them correctly.The example code is as follows: @RunWith(PowerMockRunner.class) @PrepareForTest({ClassToBeMocked.class}) public class MyTestClass { // Test code } In this example, Classtobemocked is a class that needs to be simulated. 3. Simulation static method: Use PowerMockito.when () and PowerMockito.mockStatic () to simulate the behavior of static methods.The example code is as follows: @Test public void testStaticMethod() { PowerMockito.mockStatic(ClassToBeMocked.class); PowerMockito.when(ClassToBeMocked.staticMethod()).thenReturn("mocked result"); // Ecclail operations, etc. } In this example, we first use the MockStatic () method to simulate the static method of the Classtobemocked class, and then use the where () method to specify the simulation result returned by the method. 4. Simulate private methods: Use PowerMockito.when () and PowerMockito.spy () to simulate the behavior of private methods.The example code is as follows: @Test public void testPrivateMethod() throws Exception { ClassToBeMocked mockedObject = PowerMockito.spy(new ClassToBeMocked()); String expected = "mocked result"; PowerMockito.when(mockedObject, "privateMethod").thenReturn(expected); // Ecclail operations, etc. } In this example, we first use the SPY () method to create an instance of the classstobemocked class and store it in the MockedObject variable.Then use the where () method to specify the specified simulation result when the privatemetHod () method is called when the objectmet () method is called. 5. Simulation Final Method: Use PowerMockito.when () and PowerMockito.spy () to simulate the ultimate method.The example code is as follows: @Test public void testFinalMethod() throws Exception { ClassToBeMocked mockedObject = PowerMockito.spy(new ClassToBeMocked()); String expected = "mocked result"; PowerMockito.when(mockedObject.finalMethod()).thenReturn(expected); // Ecclail operations, etc. } In this example, we also use the SPY () method to create an example of the classstobemocked class and store it in the MockedObject variable.Then use the where () method to specify when the finalMethod () method of the object is called, and return the specified simulation result. By using the PowerMock framework, in the Java class library test, we can easily simulate static, private, and final methods to solve the problems that traditional test tools cannot handle.The above is the best practice of the PowerMock framework in the Java class library test.