0
点赞
收藏
分享

微信扫一扫

Mock 测试用例

北邮郭大宝 2022-04-27 阅读 169

gradle 引入包:

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0-M1'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.7.0-M1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0-M1'
    testImplementation 'org.mockito:mockito-core:3.3.3'
    testImplementation 'org.mockito:mockito-junit-jupiter:3.3.3'
    testImplementation('com.squareup.okhttp3:mockwebserver:3.11.0') {
        exclude group: 'junit', module: 'junit'
    }
    
    testCompile group: 'org.jmockit', name: 'jmockit', version: '1.24'

静态方法 mock示例

//静态方法 when 不生效  必须使用 mockUP 重写业务
new MockUp<AppManager>(AppManager.class) {
            @Mock
            public <T extends AppInfoBase> T getAppInfo(final Class<T> cls, final long appId) {
                AppInfoDBCache appInfoDBCache = new AppInfoDBCache();
                appInfoDBCache.userCancelIsOpen = 1;
                return (T) appInfoDBCache;
            }
        };

//非静态方法直接when
when(limitProxyService.checkLimit(anyLong(), anyString(), anyLong())).thenReturn(limitRes);
举报

相关推荐

0 条评论