• mock static方法


    <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-api-mockito</artifactId>
                <version>1.6.5</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4</artifactId>
                <version>1.6.5</version>
                <scope>test</scope>
            </dependency>
    <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.3.6.RELEASE</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>1.5.2.RELEASE</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <version>${mockito.version}</version>
                <scope>test</scope>
            </dependency>
            <junit.version>4.12</junit.version>
            <mockito.version>1.10.19</mockito.version>
            <assertj.version>3.6.2</assertj.version>
    
     <!-- 测试库 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>${mockito.version}</version>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <artifactId>hamcrest-core</artifactId>
                        <groupId>org.hamcrest</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.assertj</groupId>
                <artifactId>assertj-core</artifactId>
                <version>${assertj.version}</version>
                <scope>test</scope>
            </dependency>
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.Mockito;
    import org.mockito.MockitoAnnotations;
    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.assertj.core.api.Assertions.assertThat;
    import static org.mockito.Mockito.when;
    
    
    @RunWith(PowerMockRunner.class)
    @PowerMockIgnore("javax.management.*")
    @PrepareForTest({VersionUtils.class})
    public class SettingTest {
    
    
    @Test
        public void MockTest(){
            PowerMockito.mockStatic(VersionUtils.class);
            when(VersionUtils.stringVersion2IntVersion("7.2.0")).thenReturn(70020001);
            System.out.println(VersionUtils.stringVersion2IntVersion("7.2.0"));
        }
    
    }

     mock void方法

    import static org.mockito.Mockito.doNothing;
    
    doNothing().when(mqSender).sendMessage(anyString());
  • 相关阅读:
    设计模式-Note9-行为变化类
    设计模式-Note8-数据结构类
    设计模式-Note7-状态变化类
    设计模式-Note6-接口隔离类
    设计模式--Note5--对象性能类
    设计模式--Note4--对象创建类
    设计模式--Note3--单一职责类
    设计模式--Note2--组件协作类
    V$ACCESS 查询结果慢的解决方法
    openstack numa详解(命令使用篇)
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/9144164.html
Copyright © 2020-2023  润新知