You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Mockito测试存在依赖传递的双依赖服务

Mockito测试依赖联动服务的疑问与示例

我不清楚如何用Mockito测试一个包含两个依赖的服务——第二个依赖的输入是第一个依赖的返回结果。我编写了一个示例应用,访问http://localhost:8080/countries可获取三个国家的数据,数据来自第三方国家API。我需要de.example.mockito.service.CountryService类的单元测试示例,并且认为将restCountiesClient.findCountriesByCode(countryCodes)的Mock结果直接作为参数传入同样被Mock的countryMapper.map(restCountryList)的做法不太合理。

原测试代码

@ExtendWith(MockitoExtension.class)
class CountryServiceTest {
    
    @Mock
    RestCountriesClient client;
    
    @Mock
    CountryMapper mapper;
    
    CountryService countryService;
    
    List<RestCountry> restCountryList;
    List<CountryDto> countryDtoList;
    
    final String COUNTRY_CODES = "pe,at";

    @BeforeEach
    void setUp() throws Exception {
        countryService = new CountryService(client, mapper);

        restCountryList = List.of(
                new RestCountry(new RestCountryName("Peru", "Republic of Peru")),
                new RestCountry(new RestCountryName("Austria", "Republic of Austria"))
                );
        
        countryDtoList = List.of(
                new CountryDto("Peru", "Republic of Peru"),
                new CountryDto("Austria", "Republic of Austria")
                );
    }


    @Test
    void getAllCountries() {
        given(client.findCountriesByCode(COUNTRY_CODES)).willReturn(restCountryList);
        given(mapper.map(restCountryList)).willReturn(countryDtoList);
        
        List<CountryDto> result = this.countryService.getAllCountries(COUNTRY_CODES);
        
        assertEquals(2, result.size());
        assertEquals("Peru", result.get(0).getCommonName());
        assertEquals("Republic of Peru", result.get(0).getOfficialName());
        assertEquals("Austria", result.get(1).getCommonName());
        assertEquals("Republic of Austria", result.get(1).getOfficialName());
    }
    
    @Test
    void getAllCountries2() {
        given(client.findCountriesByCode(COUNTRY_CODES)).willReturn(restCountryList);
        given(mapper.map(restCountryList)).willReturn(countryDtoList);
        
        List<CountryDto> result = this.countryService.getAllCountries2(COUNTRY_CODES);
        
        assertEquals(2, result.size());
        assertEquals("Peru", result.get(0).getCommonName());
        assertEquals("Republic of Peru", result.get(0).getOfficialName());
        assertEquals("Austria", result.get(1).getCommonName());
        assertEquals("Republic of Austria", result.get(1).getOfficialName());
    }

    @AfterEach
    void tearDown() throws Exception {
        reset(client, mapper);
    }

更新后的测试代码

@Test
    void getAllCountries() {
        given(client.findCountriesByCode(COUNTRY_CODES)).willReturn(restCountryList);
        given(mapper.map(restCountryList)).willReturn(countryDtoList);
        
        this.countryService.getAllCountries(COUNTRY_CODES);
        
        verify(client, times(1)).findCountriesByCode(COUNTRY_CODES);
        verify(mapper, times(1)).map(restCountryList);
    }
    
    @Test
    void getAllCountries2() {
        given(client.findCountriesByCode(COUNTRY_CODES)).willReturn(restCountryList);
        given(mapper.map(restCountryList)).willReturn(countryDtoList);
        
        List<CountryDto> result = this.countryService.getAllCountries2(COUNTRY_CODES);
        
        assertEquals("Other Name", restCountryList.get(0).getName().getCommon());
        verify(client, times(1)).findCountriesByCode(COUNTRY_CODES);
        verify(mapper, times(1)).map(restCountryList);
    }

内容的提问来源于stack exchange,提问作者Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 11:45:57