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

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 关于推送项目到github的问题
  • ¥15 急!C++指针编写相关的问题
  • ¥30 用python写一个多签情况下波场的代理资源和回收资源
  • ¥15 怎么在matlab中输出显示泵的流量-扬程和管路损失与流量均在一个表格里
  • ¥15 matlab学期例题代码答疑
  • ¥15 在线手电筒追加按钮JS
  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 cuda安装使用问题