PowerMock 使用示例

记录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.csdn.net/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);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十年梦归尘

愿意支持一下不(*^▽^*)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值