首页 > 解决方案 > 如何在一种方法中模拟两个 Uri 组件

问题描述

我在 addnewMap() 中的一种方法中有两个 url 调用 - 一个是 buildGetSubtenantsURL,另一个是 buildGetAssetsURL

public void addNewMap(MapDTO mapDTO) {
        log.info("going to add the map data into db");
        if (mapRepository.existsMapWithMapName(mapDTO.getMapName()))
            throw new BadRequestException("Map with map name " + mapDTO.getMapName()
                    + " already exists. Please provide a different map name.");
        Map<String, String> subtenantInfoMap = new HashMap<>();
        Maps mapEntity = new Maps();
        String iottenant = mapDTO.getTenant();
        String subtenantsURL = buildGetSubtenantsURL(null);
        String subTenantsResponse  = getSubtenants(subtenantsURL,iottenant);
        JSONObject subTenant = getSubtenantName(subTenantsResponse);
        checkForMultiplePagesSubtenants(subTenantsResponse, subtenantInfoMap,iottenant);
        
        if(subtenantInfoMap.get(mapDTO.getSubtenantName()) != null) {
            mapEntity = Maps.builder().subtenant(subtenantInfoMap.get(mapDTO.getSubtenantName()).toString()).build();
        }
        else {
            throw new DataNotFoundException(SUBTENANT_DOESNT_EXIST);
        }
        
        String SubtenantId = subtenantInfoMap.get(mapDTO.getSubtenantName());
        UriComponents assetsURL = buildGetAssetsURL(iottenant,SubtenantId);
        String assetsResponse = getAssets(assetsURL, iottenant);
        String mindsphereAssetId = getAssetId(assetsResponse);
        
        if(mindsphereAssetId.isEmpty()) {
            throw new DataNotFoundException(ASSET_ID_DOESNT_EXIST);
        }
        else {
            mapEntity = Maps.builder().mindsphereAssetId(mindsphereAssetId).build();
        }
        
        
        mapEntity = Maps.builder().mapName(mapDTO.getMapName()).displayName(getDisplayName(mapDTO))
                .description(Objects.nonNull(mapDTO.getDescription()) ? mapDTO.getDescription() : null)
                .tenant(getTenantNameForMapDTO(mapDTO)).mindsphereAssetId(mindsphereAssetId).subtenant(subtenantInfoMap.get(mapDTO.getSubtenantName()).toString())
                .mapLocation(mapDTO.getMapLocation()).operator(mapDTO.getOperator()).recipeName(mapDTO.getRecipeName()).subtenantName(mapDTO.getSubtenantName())
                .createdBy(getUserEmail()).createdAt(new Timestamp(System.currentTimeMillis()))
                .build();
        Maps createdMap = mapRepository.saveAndFlush(mapEntity);
        addStationsMappingforNewMap(createdMap);
    }

我已将上述方法的测试用例编写为:

@Test
     public void addNewMap() {
         
         map = Maps.builder().mapId(1l).mapName("testMap").displayName("Map Test").mindsphereAssetId("a0609ebf2eb7400da8a5fd707e7f68b7").mapLocation("hyd").operator("operator").recipeName("recipe").subtenantName("NSTI").tenant("ctlbrdev").subtenant("9b04027dde5fbd047073805ab8c1c87c")
                    .tenant(Tenant).build();
            maps = Arrays.asList(map);
            mapDTO = MapDTO.builder().mapId(1l).mapName("testMap").displayName("Map Test").subtenantName("NSTI").mapLocation("hyd").recipeName("recipe").operator("operator").description("description")
                    .tenant("ctlbrdev").build();
            ReflectionTestUtils.setField(mapService, "mindsphereBaseURL", MindsphereBaseURL);
            ReflectionTestUtils.setField(mapService, "mindsphereSubtenantsURL", mindsphereSubtenantsURL);
            ReflectionTestUtils.setField(mapService, "mindsphereAssetsURL", mindsphereAssetsURL);
            when(restTemplate.exchange(ArgumentMatchers.anyString(), ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(HttpEntity.class), 
                    ArgumentMatchers.<Class<String>>any()))
            .thenReturn(new ResponseEntity<String>(entityDtoCreaters.getSubtenant(),HttpStatus.OK));
            when(tokenCaching.retrieveHeadersContainingTechToken("ctblrdev")).thenReturn(new HttpHeaders());
      when(mapRepository.existsMapWithMapName(any())).thenReturn(false); 
      //doReturn(Tenant).when(mapService).getTenantName();
      doReturn(EMAIL).when(mapService).getUserEmail();
      when(mapRepository.saveAndFlush(any())).thenReturn(map);
      when(restTemplate.exchange(ArgumentMatchers.anyString(), ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(HttpEntity.class), 
                ArgumentMatchers.<Class<String>>any()))
        .thenReturn(new ResponseEntity<String>(entityDtoCreaters.getSubtenant(),HttpStatus.OK));
      Map<String, String> subtenantInfoMap = new HashMap<>();
      subtenantInfoMap.get(mapDTO.getSubtenantName());
      mapService.addNewMap(mapDTO);

它没有覆盖getAssets()方法,因此没有覆盖整个方法。我怎样才能做到这一点?

标签: javaspringspring-bootmockitojunit4

解决方案


推荐阅读