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

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条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

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

      悬赏问题

      • ¥20 b站私信完整导出的方法
      • ¥15 边界图中的x、y轴解释
      • ¥30 如何用matlab求解复杂方程
      • ¥15 VUE TS中怎么调用第三方js
      • ¥15 Activiti如何将值传入textValue
      • ¥20 在linux里安装lummps时 在make mpi里找不到rpc
      • ¥25 python先到先得调度和轮询调度
      • ¥15 SDK Manager闪退
      • ¥15 intel i219v mac地址修改问题
      • ¥15 leetcode上面的题,求解