PowerMockitoの使用例

4477 ワード

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 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> map = new HashMap<>();
        Map 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 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());
    }

}