可选类型的单元测试用例



这是我的原始方法:

public Unit getUnitSymbolForCRSCode(Integer crsCode) {  
String crsUnitName = getCrsByCode(crsCode).getUnitName();
List<Unit> unitList = getUnits();
Optional<Unit> unit = unitList.stream().filter(u->u.getUnitOfMeasureName().equalsIgnoreCase(crsUnitName)).findFirst();
if(!unit.isPresent()){
throw new DataNotFoundException(String.format("Failed to retrieve unit details for %s.",crsUnitName));
}
return unit.get();
}

在为它编写如下所示的测试用例时,未涵盖一个分支。我无法抛出 DataNotFoundException。

@Test
public void testGetUnitSymbolForCRSCodeThrowingDataNotFoundException() {
Unit unitObj = new Unit();
Mockito.when(geoCalService.search(Mockito.any(SearchFilter.class)))
.thenReturn(TestDataFactory.getSearchResultResponseForCRS());
Mockito.when(uomService.getUnits()).thenReturn(Arrays.asList(unitObj));
thrown.expect(DataNotFoundException.class); 
shellGeodeticService.getUnitSymbolForCRSCode(50015);
} 

我收到类似错误

java.lang.AssertionError: Expected test to throw an instance of com.shell.geodetic.exception.DataNotFoundException. 

虽然UnitObj是空的,但它不会抛出DataNotFoundException。请协助。

public static List<Unit> getUnitList() {
List<Unit> unitList= new ArrayList<Unit>();
unitList.add(new Unit("dega","Degree"));
unitList.add(new Unit("ft[US]","US Survey foot"));
unitList.add(new Unit("m","Meter"));
unitList.add(new Unit("ft[Se]","Sear's Foot"));
unitList.add(new Unit("ft[GC]","Gold Coast Foot"));
unitList.add(new Unit("ft","International Foot"));      
unitList.add(new Unit("link[Cla]","Clarke's Link"));
unitList.add(new Unit("gon","Grad"));
return unitList;
}

public CRS getCrsByCode(Integer code) {
SearchResultResponse response = searchCode(String.valueOf(code), 180224);
List<DisplayItem> crsDisplayItems = response.getDisplayItems();
if (crsDisplayItems.isEmpty()) {
throw new DataNotFoundException("CRS not found with code " + code + ": " + response.getSearchMessage());
}
return Util.convertToCrsVoList(crsDisplayItems).get(0);
}

你把我们送进了一个兔子洞,越来越多的方法最终提供了一些数据。

这是你通常如何写这种东西。

class MyService {
CrsService crsService;
UnitService unitService;
public Unit getUnitSymbolForCRSCode(Integer crsCode) {  
String crsUnitName = crsService.getCrsByCode(crsCode).getUnitName();
return unitService.getUnits().stream()
.filter(u->u.getUnitOfMeasureName().equalsIgnoreCase(crsUnitName))
.findFirst()
.orElseThrow(() -> 
new DataNotFoundException(String.format(
"Failed to retrieve unit details for %s.",crsUnitName));
}

这就是你测试它的方式(JUnit 5(:

@ExtendWith(MockitoExtension.class)
class MyServiceTest {
@Mock crsService;
@Mock unitService;
@InjectMocks MyService;
@Test
void testNoDataException() {
CRS crs = mock(CRS.class);
when(crsService.getCrsByCode(any())).thenReturn(crs);
when(unitService.getUnits()).thenReturn(Collections.emptyList());
assertThrows(DataNotFoundException.class,
() -> sut.getUnitSymbolForCRSCode(123));
}
}

为了完整起见,这将是您稍后发布的CrsServiceUnitService

class FixedUnitService implements UnitService {
public List<Unit> getUnits() {
List<Unit> unitList= new ArrayList<Unit>();
unitList.add(new Unit("dega","Degree"));
unitList.add(new Unit("ft[US]","US Survey foot"));
unitList.add(new Unit("m","Meter"));
unitList.add(new Unit("ft[Se]","Sear's Foot"));
unitList.add(new Unit("ft[GC]","Gold Coast Foot"));
unitList.add(new Unit("ft","International Foot"));      
unitList.add(new Unit("link[Cla]","Clarke's Link"));
unitList.add(new Unit("gon","Grad"));
return unitList;
}
}
class LookupCrsService implements CrsService {
public Crs getCrsByCode(int id) {
SearchResultResponse response = searchCode(String.valueOf(code), 180224);
List<DisplayItem> crsDisplayItems = response.getDisplayItems();
if (crsDisplayItems.isEmpty()) {
throw new DataNotFoundException("CRS not found with code " + code + ": " + response.getSearchMessage());
}
return Util.convertToCrsVoList(crsDisplayItems).get(0);
}
}

您可以完全单独测试这些类。

我相信,最简单的方法是有一个unitList,例如filter操作后流将为空,即它的任何单元都不应该具有相同的度量名称。

在这种情况下findFirst将返回其文档中所述的Optional.empty()

最简单的方法是做:

Mockito.when(uomService.getUnits()).thenReturn(Collections.emptyList()))

最新更新