我有一個 Java 服務,它咨詢資料庫并回傳一個串列,在我的服務中我呼叫我的 Util 類的方法validacionCampos(),這個類驗證我的服務串列的欄位是否具有空值或值為 0設定為 0.000
我的目標是擁有一個涵蓋所有服務方法和 util 類驗證的測驗用例。我做了一個測驗類,但它根本不起作用,因為 sonarqube 工具說它沒有完全覆寫并指向 util 類的if行,我做了兩個方法,一個設定 null 值,另一個設定值 0 ,但我認為這是錯誤的或什么都不做
任何人都可以幫助我,測驗用例是如何完成的,以便它進入服務和有用的類并且覆寫完全?
ServiceImpl 類
@Override
public ResponseEntity<?> consultarReportes(Integer fechaInicio, Integer fechaFin) throws Exception {
Map<String, Object> response = new HashMap<>();
List<EntityDa> consultarReporte = new ArrayList<EntityDa>();
try {
consultarReporte = bitacoraRepository
.consultarBitacoras(fechaInicio, fechaFin);
reporteUtil.validacionCampos(consultarReporte);
} catch (Exception e) {
LOGGER.error("An error ocurred looking for Entity Data");
response.put("success", false);
response.put("error", e.getMessage());
return new ResponseEntity<Map<String, Object>>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
response.put("success", true);
response.put("data", consultarReporte);
return new ResponseEntity<>(response, HttpStatus.OK);
}
實用類
private static final String DECIMAL = "0.0000";
public List<EntityDa> validacionCampos(List<EntityDa> lista) {
for (EntityDa details : lista) {
if (details.getRango() == null || details.getRango().equals("0")) {
details.setRango(DECIMAL);
}
if (details.getPrima() == null || details.getPrima().equals("0")) {
details.setPrima(DECIMAL);
}
}
return lista;
}
測驗班
@Mock
BitacoraRepository bitacoraRepository;
@Test
void testExitoValidaNullos() throws Exception {
EntityDa entity = new EntityDa();
entity.setRango(null);
entity.setPrima(null);
List<EntityDa> list = new ArrayList<>();
list.add(entity);
when(bitacoraRepository.consultarBitacoras("20220112", "20220112")).thenReturn(list);
}
@Test
void testExitoValidaVacios() throws Exception {
EntityDa entity = new EntityDa();
entity.setRango("0");
entity.setPrima("0");
List<EntityDa> list = new ArrayList<>();
list.add(entity);
when(bitacoraRepository.consultarBitacoras("20220112", "20220112")).thenReturn(list);
}
更新
我將它作為您的示例實作并且它保持這樣,因為 IDE 向ResponseEntity<Map<String, Object>>添加了一個強制轉換,因為我的服務被宣告為 ResponseEntity<?>
@Test
public void testExitoValidaNullos() throws Exception {
// Given a repository containing an entity with "rango" and "prima" set to null
EntityDa entity = new EntityDa();
entity.setRango(null);
entity.setPrima(null);
List<EntityDa> list = new ArrayList<>();
list.add(entity);
when(bitacoraRepository.consultarBitacoras("20220112", "20220112")).thenReturn(list);
// When consultarReportes is called
ResponseEntity<Map<String, Object>> response = (ResponseEntity<Map<String, Object>> serviceImpl.consultarReportes("20220112", "20220112");
// Then the returned entity should have "rango" and "prima" set to "0.0000"
EntityDa returnedEntity = ((List<EntityDa>) response.getBody().get("data")).get(0);
assertEquals("0.0000", returnedEntity.getRango());
assertEquals("0.0000", returnedEntity.getPrima());
}
我運行了測驗,它發送了這個:
org.opentest4j.AssertionFailedError:預期:<0.0000> 但原為:<41831.3>
顯然它沒有將設定設為空
uj5u.com熱心網友回復:
SonarQube 報告沒有覆寫這些行,因為這些測驗實際上并未執行此代碼。他們執行一些設定,但不呼叫任何方法ServiceImpl。
將測驗視為具有三個部分很有用,通常稱為given、when和then:
- Given表示測驗用例的背景關系或環境
- 何時描述所采取的操作 - 正在測驗的操作
- 然后描述預期或預期的結果
使用書面語言以這種格式寫出需求,然后將書面需求翻譯成代碼可能會有所幫助。
例如,我可能會為這些測驗用例撰寫需求(英文):
- 給定一個包含“范圍”
null或“0”的物體的存盤庫, - 什么時候
consultarReportes叫, - 然后回傳的物體應該將“rango”設定為“0.0000”
和:
- 給定一個包含“prima”
null或“0”物體的存盤庫, - 什么時候
consultarReportes叫, - 然后回傳的物體應該將“prima”設定為“0.0000”
在代碼方面:
- Given經常轉化為設定資料,有時也轉化為模擬。
- 何時呼叫被測方法。
- 然后通常是關于回傳值的斷言,或受方法影響的其他狀態。它也可以是在模擬物件上呼叫的方法的驗證,特別是當您測驗一個沒有回傳值的方法時,它只會執行副作用。
在這種情況下,測驗可能如下所示:
@Test
public void testExitoValidaNullos() throws Exception {
// Given a repository containing an entity with "rango" and "prima" set to null
EntityDa entity = new EntityDa();
entity.setRango(null);
entity.setPrima(null);
List<EntityDa> list = new ArrayList<>();
list.add(entity);
when(bitacoraRepository.consultarBitacoras("20220112", "20220112")).thenReturn(list);
// When consultarReportes is called
ResponseEntity<Map<String, Object>> response = serviceImpl.consultarReportes("20220112", "20220112");
// Then the returned entity should have "rango" and "prima" set to "0.0000"
EntityDa returnedEntity = ((List<EntityDa>) response.getBody().get("data")).get(0);
assertEquals("0.0000", returnedEntity.getRango());
assertEquals("0.0000", returnedEntity.getPrima());
}
對 testExitoValidaVacios 的更改將是類似的。
(我對提供的代碼進行了一些其他更改以使其能夠編譯。)
為了完全涵蓋所有可能性,您還需要為這些欄位設定為其他值時撰寫測驗用例。您可能還想通過存根bitacoraRepository.consultarBitacoras拋出例外來測驗例外處理程式。
請注意,直接針對類測驗這些案例可能會更簡單Util。這將需要更少的設定(不需要模擬)并且不需要從回應中解壓縮物體。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/419468.html
標籤:
