如果Mockito有两个依赖项,而第二个依赖项应该与第一个依赖项的结果一致,我不知道如何使用它来测试服务。
为了更好地描述我的问题,我为此写了一个小应用程序:https://github.com/MartinHein-dev/mockito-example
与http://localhost:8080/countries一个得到三个国家的结果https://restcountries.com/
如果你能告诉我de.example.mokito.service.CountryService.class的单元测试会是什么样子,我将非常高兴。
继续使用此.restCountiesClient.findCountriesByCode(countryCodes(的模拟结果并将其用作此.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);
}
更新测试(2(:
@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);
}
当您测试getAllCountries
时,您正在测试数据流是否在方法中工作,因为方法本身除了在依赖项之间传递数据外,什么都不做。因此,您不需要为该方法设置填充的对象,也不需要断言返回的对象包含任何特定的填充数据。您只需要验证预期的(模拟的(依赖关系是用预期的对象引用调用的。
测试您在测试中设置的值属于以CountryMapper
为目标的单元测试。
对于getAllCountries2
,您必须验证测试数据中的值是否如您所期望的那样发生了更改,但同样不需要验证其他值。