Article catalog
PowerMockito.mockStatic(class) simulate static method call
Why write unit tests
- give us the confidence to refactor. A bunch of tangled and untested code, do you dare to modify it?
- A good unit test is a document expected behavior. A few practical examples are more interesting than documents!
Introduce dependency
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.23.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>2.0.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.0</version> <scope>test</scope> </dependency>
Simulation tool class calls static methods
Tool class: SysDataCodeUtils
@Slf4j public class SysDataCodeUtils { public static String getNameByCodeAndValue(String code, String key) { try { List<SysDataCodeDTO> list = sysDataCodeService.getListByCode(code); for (SysDataCodeDTO dataCodeDTO : list) { if (dataCodeDTO.getCode().equals(key)) { return dataCodeDTO.getValue(); } } } catch (Exception e) { log.error("Get quick code table exception:{}", e); } return StrUtil.EMPTY; } }
Test class: ShareProfitRefundSettleServiceImplTest
@RunWith(PowerMockRunner.class) @PrepareForTest({ SysDataCodeUtils.class }) public class ShareProfitRefundSettleServiceImplTest { @InjectMocks ShareProfitRefundSettleServiceImpl shareProfitRefundSettleService = new ShareProfitRefundSettleServiceImpl(); @Mock ClearingDao clearingDao; @Before public void before(){ log.info("start--------{}",this.getClass().getName()); PowerMockito.mockStatic(SysDataCodeUtils.class); PowerMockito.when(SysDataCodeUtils.getNameByCodeAndValue("SHARE_PROFIT_REFUND_TERM", "SWITCH")).thenReturn("OPEN"); } }
Notes
@RunWith( MockitoJUnitRunner.class ): JUnit uses the "unit test runner" provided by the Mockito simulation framework.
@RunWith( PowerMockRunner.class ): JUnit uses the unit test runner in the PowerMock framework.
The point of using these framework specific test runners is that they are responsible for initializing all fields with special framework specific comments.
So the only difference here is that the first example is written using the Mockito framework, and the second example uses PowerMock
Written. *If you want to simulate a call to a static method, you need to use @ runwith( PowerMockRunner.class ). If you don't need to simulate static or private functions, it's best to use the native mockito JUnit runner for a single test.
@PrepareForTest({ SysDataCodeUtils.class }): when using PowerMockito.whenNew Method must be annotated with @ preparefortest and @ RunWith. The class written in the annotation @ preparefortest is the class where the new object code requiring mock is located. If there are two different static methods to verify, such as class A and class B, you only need to add them together and separate them with commas: @ preparefortest ({a.class, b.class}).
@InjectMocks: create an instance. In short, this mocks can call methods of real code. The rest mocks created with @ mocks (or @ Spy) annotation will be injected into this instance.
@Mock: create a mock.