简体   繁体   中英

How to write mockito test cases for factory class

I was new for writing test cases. I want to write the Mockito test cases for the below :

@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;
    }
}

Can anyone please help me with the code sample to write mockito test cases for the factory class.

The only thing you might want to test in this class is the public convertEventToEntity method. The class has two dependencies - dataService and modelMapper . But they are not used in the code, so you don't need to mock them. So you can write tests for this class without using Mockito. Just create an object of FactoryClass and call the method with some input CEvent and Event objects. You can assert on expected List<TestEntity> .

You can test several logical paths in this method. For example, there's an if condition that does something only if cEvent's order details are present. So you can have two test cases where you pass cEvent with and without order details and you can verify that the code is executed correctly in both cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM