In OOP, testing classes often involves stubbing the dependencies.
A common example is that of Service / DAO (Data access object) pattern:
Consider the implementation of FruitService below:
public class FruitServiceImpl implements FruitService {
private final FruitRepository fruitRepository;
public FruitServiceImpl(FruitRepository fruitRepository) {
this.fruitRepository = fruitRepository;
}
@Override
public List<Fruit> getFruitsInStock() {
return fruitRepository.getAllFruits().stream()
.filter(fruit -> fruit.getQtyInStock() > 0)
.collect(Collectors.toList());
}
}
Testing the “getFruitsInStock” method would mean asserting that only fruits whose qtyInStock > 0 is returned.
Logically, this can be tested without any concrete implementation of FruitRepository being provided.
However, DiffBlue cover fails to generate a test for this method stating:
Cannot generate essential test for getFruitsInStock: Unable to generate test inputs not throwing a trivial exception.
Diffblue Cover could not determine valid inputs for the methods under test.
I think the tool should be able to generate meaningful stubs where possible. Is this a known limitation or am I missing something ?