PowerMockito用法示例

本文通过单元测试验证了深智控制台配置服务中OCR配置构建和乳腺MG检测配置处理的功能。测试覆盖了不同配置场景下服务的响应,确保了配置的正确性和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.deepwise.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.deepwise.constant.AisvrConstants;
import com.deepwise.constant.ConfigKeyConstants;
import com.deepwise.main.DeepWiseConsoleServer;
import com.deepwise.model.param.AisvrConfigForm;
import com.deepwise.service.ConfigService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
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 org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 
 * @date 2019/2/26
 * @description
 */
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore({"javax.management.*", "javax.net.ssl.*"})
@SpringBootTest(classes = DeepWiseConsoleServer.class)
@PrepareForTest({ServerConfigServiceImpl.class})
@Rollback
public class ServerConfigServiceImplTest {

    @Autowired
    private ServerConfigServiceImpl serverConfigService;

    ServerConfigServiceImpl serverConfigServiceImpl = PowerMockito.mock(ServerConfigServiceImpl.class);

    @MockBean
    private ConfigService configService;

    @Test
    @Transactional
    public void testBuildOcrConfig() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put(AisvrConstants.SHOW_OCR_STUDY_LIST, true);
        PowerMockito.when(configService.getValueByKey(ConfigKeyConstants.GLOBAL_WEBVIEW_SETTINGS))
                .thenReturn(JSONObject.toJSONString(map));
        PowerMockito.when(serverConfigServiceImpl, "buildOcrConfig", new AisvrConfigForm()).thenCallRealMethod();
        AisvrConfigForm form = serverConfigService.getAisvrConfig();
        Assert.assertNotNull(form.getOcrConfig());
        Assert.assertEquals(true, form.getOcrConfig().getShowOcrStudyList());

        map.clear();//清空map
        PowerMockito.when(configService.getValueByKey(ConfigKeyConstants.GLOBAL_WEBVIEW_SETTINGS))
                .thenReturn(JSONObject.toJSONString(map));
        PowerMockito.when(serverConfigServiceImpl, "buildOcrConfig", new AisvrConfigForm()).thenCallRealMethod();
        AisvrConfigForm form02 = serverConfigService.getAisvrConfig();
        Assert.assertNotNull(form02.getOcrConfig());
        Assert.assertEquals(false, form02.getOcrConfig().getShowOcrStudyList());
    }

    @Test
    @Transactional
    public void testHandleBreastMGDetectConfig() throws Exception {
        Map<String, Map<String, Object>> map = new HashMap<>();
        Map<String, Object> m01 = new HashMap<>();
        m01.put(AisvrConstants.IS_ABNORMAL_DIRECTION, false);
        m01.put(AisvrConstants.SIDE_TO_TURN, "rightOrleft");
        //默认旋转180°
        m01.put(AisvrConstants.TURNING_MODE, 0);
        map.put(AisvrConstants.DEAL_WITH_SPECIAL_DATA, m01);

        Map<String, Object> m02 = new HashMap<>();
        m02.put(AisvrConstants.ISNOT_TO_SAVE_ABNORMAL_DATA, false);
        m02.put(AisvrConstants.TAG_WITH_ABNORMAL_DATA, "");
        m02.put(AisvrConstants.CONTENT_WITH_ABNORMAL_DATA_TAG, "hello world");
        map.put(AisvrConstants.EXCLUDE_ABNORMAL_DATA_CONFIG, m02);

        PowerMockito.when(configService.getValueByKey(ConfigKeyConstants.CONSOLE_BREAST_MG_DETECT_CONFIG))
                .thenReturn(JSONObject.toJSONString(map));
        PowerMockito.when(serverConfigServiceImpl, "handleBreastMGDetectConfig", new AisvrConfigForm()).thenCallRealMethod();
        AisvrConfigForm form01 = serverConfigService.getAisvrConfig();
        AisvrConfigForm.BreastMGDetectConfig config = form01.getBreastMGDetectConfig();
        Assert.assertNotNull(config);
        Assert.assertEquals("rightOrleft", config.getSideToTurn());
        Assert.assertEquals("hello world", config.getContentWithAbnormalDataTag());
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值