sunyuan0827 2022-07-24 11:07 采纳率: 50%
浏览 424
已结题

junit单元测试,mock数据后,调用时返回数据为null

junit单元测试,mock数据后,调用时返回数据为null
问题相关代码,请勿粘贴截图
package com.zdicc.online.core.service;

import com.alibaba.fastjson.JSONObject;
import com.zdicc.online.core.ZdiccApplicationTests;
import com.zdicc.online.core.dto.DatasourceDto;
import java.util.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.web.client.RestTemplate;

@AutoConfigureMockMvc
public class DatasourceServiceTest extends ZdiccApplicationTests {

  @MockBean
  private RestTemplate restTemplate;
  @Autowired
  private DatasourceService datasourceService;

  @BeforeEach
  public void init() {
    MockitoAnnotations.initMocks(this);
  }

  @Test
  public void remoteGetDatasourceByTypeIds() {
    Map<String, List<Integer>> map = new HashMap<>();
    map.put("job", Collections.singletonList(105));
    map.put("api", Collections.singletonList(101));

     String response = "{\n" +
         "\t\"status\": 1,\n" +
         "\t\"data\": [{\n" +
         "\t\t\"dbType\": \"mysql\",\n" +
         "\t\t\"nodeType\": \"api\",\n" +
         "\t\t\"originId\": 101,\n" +
         "\t\t\"sourceType\": 1,\n" +
         "\t\t\"url\": \"192.168.100.134:3306/demo.create_tables\"\n" +
         "\t}, {\n" +
         "\t\t\"dbType\": \"mysql\",\n" +
         "\t\t\"nodeType\": \"job\",\n" +
         "\t\t\"originId\": 195,\n" +
         "\t\t\"sourceType\": 1,\n" +
         "\t\t\"url\": \"192.168.100.134:3306/demo.vote_demo\"\n" +
         "\t}, {\n" +
         "\t\t\"dbType\": \"hive\",\n" +
         "\t\t\"nodeType\": \"job\",\n" +
         "\t\t\"originId\": 195,\n" +
         "\t\t\"sourceType\": 2,\n" +
         "\t\t\"url\": \"hello.demo_vote_demo_hourly_vera2\"\n" +
         "\t}, {\n" +
         "\t\t\"dbType\": \"mysql\",\n" +
         "\t\t\"nodeType\": \"job\",\n" +
         "\t\t\"originId\": 150,\n" +
         "\t\t\"sourceType\": 1,\n" +
         "\t\t\"url\": \"192.168.100.134:3306/demo.vote_demo\"\n" +
         "\t}, {\n" +
         "\t\t\"dbType\": \"hive\",\n" +
         "\t\t\"nodeType\": \"job\",\n" +
         "\t\t\"originId\": 150,\n" +
         "\t\t\"sourceType\": 2,\n" +
         "\t\t\"url\": \"default.david_test_vote_demo\"\n" +
         "\t}]\n" +
         "}";

    JSONObject jsonObject = JSONObject.parseObject(response);
    Mockito.when(this.restTemplate.postForObject(Mockito.any(), Mockito.anyList(), Mockito.any()))
        .thenReturn(jsonObject);

    Map<String, List<DatasourceDto>> resMap = datasourceService.remoteGetDatasourceByTypeIds(map);
    Assertions.assertNotNull(resMap);
  }
}


运行结果及报错内容
 public Map<String, List<DatasourceDto>> remoteGetDatasourceByTypeIds(Map<String, List<Integer>> typeIdsMap) {
    if (typeIdsMap == null || typeIdsMap.isEmpty()) {
      return null;
    }

    String url = onlineEnvConfig.getDAAM_BASE_URL() + DAAM_DATASOURCE_URI;
    List<Map<String, Object>> paramMap = new ArrayList<>();
    Map<String, Object> subMap = null;
    for (String nodeType : typeIdsMap.keySet()) {
      subMap = new HashMap<>();
      subMap.put("nodeType", nodeType);
      subMap.put("originalIdList", typeIdsMap.get(nodeType));
      paramMap.add(subMap);
    }
    JSONObject response = null;
    try {
      response = restTemplate.postForObject(url, paramMap, JSONObject.class);
      //这里返回null,并没有返回mock的内容
    } catch (RestClientException e) {
      log.error("Failed to request step datasource detail, e = {}", e.getMessage(), e);
      // FIXME: 2021/2/26 是否需要直接报错
      //            throw new OnlineException(ResponseEnum.REST_TEMPLATE_REQUEST_FAIL);
    }

  • 写回答

1条回答 默认 最新

  • eq0284 2022-07-24 11:31
    关注

    你好像没将datasourceService.remoteGetDatasourceByTypeIds方法的结果返回出去

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 8月2日
  • 已采纳回答 7月25日
  • 创建了问题 7月24日

悬赏问题

  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题