TA的每日心情 | 无聊 4 天前 |
---|
签到天数: 530 天 连续签到: 2 天 [LV.9]测试副司令
|
1测试积点
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> 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);
- }
-
复制代码
|
|