记录PowerMock使用的示例,供自己使用参考
pom.xml 中引入包
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito2 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.ArgumentMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* 示例
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.script.*", "javax.management.*", "org.w3c.dom.*", "org.apache.log4j.*", "org.xml.sax.*", "javax.xml.*"})
@PrepareForTest({CurrentUserInfo.class})
public class DestinationFlightConfigurationRulesServiceTest {
@InjectMocks
private DestinationFlightConfigurationRulesService destinationFlightConfigurationRulesService;
@Mock
DestinationFltRuleMapper destinationFltRuleMapper;
@Mock
FlightMapperExt flightMapperExt;
@Mock
DestinationFlightConfigurationRulesMapperExt destinationFlightConfigurationRulesMapperExt;
DestinationFltRuleObjectDto destinationFltRuleObjectDto;
DestinationFltRule destinationFltRule;
DestinationFltRuleDto fltRuleDto;
String portName = "portName";
@Before
public void setUp() throws Exception {
// Setup
destinationFltRuleObjectDto = new DestinationFltRuleObjectDto();
fltRuleDto.setNotDestinationSite("notDestinationSite");
}
/*mock静态方法步骤
使用PowerMockito.mockStatic()
加上@RunWith和@PrepareForTest注释
如果报错下面的错则考虑加上@PowerMockIgnore
也可用使用mockito测试静态方法。但是对版本是有要求的。
首先需要确认下mockito是可以进行静态方法的Mock的操作的,在网上看了很多的帖子都说需要集成PowerMockito才可以Mock静态方法,
确实通过PowerMockito可以完成静态方法,但是在版本3.4.0中,Mockito现在支持Mock静态方法。
并且作为SpringBootTest默认集成的Mock工具,还是更建议大家使用高版本的mockito,并通过它来完成静态方法的Mock。
具体参考:
https://blog.youkuaiyun.com/Octopus21/article/details/118486032 */
@Test
public void testFindByFlightId() throws Exception {
// setUp
//mock静态方法
PowerMockito.mockStatic(CurrentUserInfo.class);
when(CurrentUserInfo.getPortName()).thenReturn(portName);
doReturn(destinationFltRule).when(destinationFltRuleMapper).selectByPrimaryKey(anyLong());
Result result = destinationFlightConfigurationRulesService.findByFlightId(destinationFltRuleObjectDto);
System.out.println(result.getMsg());
//System.out.println(new Gson().toJson(result.getData()));
// Verify the results
assertNotNull(result);
assertEquals(result.getCode(),200);
}
@Test
public void testVerifyDestinationSite_Success() {
// Run the test
fltRuleDto.setDestinationSite("PVG");
Result result = destinationFlightConfigurationRulesService.verifyDestinationSite(fltRuleDto);
System.out.println(result.getMsg());
// Verify the results
assertNotNull(result);
assertEquals(result.getCode(),200);
}
@Test
public void testUpdateOrAddFlight() throws Exception {
DictionaryEntries dictionaryEntries = new DictionaryEntries();
dictionaryEntries.setId(999L);
CacheSystem.dictionaryEntriesMap.put(GlobalConstantFedex.FLIGHT_STATUS_DELETED,dictionaryEntries);
doReturn("UA858").when(flightMapperExt).findSourceFlightExits(anyString(),anyString(),anyLong());
PowerMockito.mockStatic(CurrentUserInfo.class);
SysUser sysUser = new SysUser();
sysUser.setId(888L);
sysUser.setUsername("mockSysUser");
when(CurrentUserInfo.getUser()).thenReturn(sysUser);
doReturn(destinationFltRule).when(destinationFlightConfigurationRulesMapperExt).findByID(anyLong());
doReturn(1).when(destinationFltRuleMapper).updateByPrimaryKeySelective(any(DestinationFltRule.class));
doReturn(1).when(destinationFltRuleMapper).insert(any(DestinationFltRule.class));
Result updateResult = destinationFlightConfigurationRulesService.updateOrAddFlight(fltRuleDto,portName);
System.out.println(updateResult.getMsg());
assertNotNull(updateResult);
assertEquals(updateResult.getCode(),200);
}
}