![](/img/trans.png)
[英]How to write test cases for this fee calculator service using Junit / Mockito?
[英]How to write mockito test cases for factory class
我是新來編寫測試用例的人。 我想為以下內容編寫Mockito測試用例:
@Component
public class FactoryClass {
@Autowired
private DataService dataService;
@Autowired
private ModelMapper modelMapper;
public List<TestEntity> convertEventToEntity(CEvent cEvent, Event event){
List<TestEntity> TestEntityList =new ArrayList<>();
if (!CollectionUtils.isEmpty(cEvent.getOrderDetails())) {
for (CEventDetail cEventDetail : cEvent.getCEventDetail()) {
log.info("Creating TestEntity entity ..");
TestEntity testEntity = new TestEntity();
testEntity.setEId(event.getEId());
testEntity.setActId(event.getHeaderReference().getActId());
testEntity.setEName(event.getEType());
tfbUpgrades.setPO(cEventDetail.getPO());
TestEntityList.add(testEntity);
}
}
return TestEntityList;
}
}
誰能幫我提供代碼示例,以為工廠類編寫模擬測試用例。
您可能要在此類中測試的唯一東西是公共convertEventToEntity
方法。 這個類有兩個依賴- dataService
和modelMapper
。 但是它們未在代碼中使用,因此您無需模擬它們。 因此,您可以在不使用Mockito的情況下為此類編寫測試。 只需創建一個FactoryClass對象,並使用一些輸入的CEvent和Event對象調用該方法。 您可以在期望的List<TestEntity>
上斷言。
您可以使用此方法測試多個邏輯路徑。 例如,有一個if條件,僅當cEvent的訂單詳細信息存在時才起作用。 因此,您可以有兩個測試用例,分別傳遞帶有和不包含訂單詳細信息的cEvent,並且可以驗證兩種情況下代碼均正確執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.