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

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日

悬赏问题

  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化