Diffblue Cover should provide meaningful stubs for object dependencies

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 ?

Thanks for your concise example – short answer is yes, it is a known limitation of the current product and we have been thinking about how to solve for this and similar cases.

I’m sorry but this makes DiffBlue Cover totally unusable in real world scenarios. If DiffBlue is not dependency injection aware, how can it ever generate any meaningful tests?

I paid for a subscription and ran it on a spring boot (spring batch) application and for 99% of the methods that I tried to generate a test for it gave me the error referenced above.

1 Like

@pojomojo, if you use Spring Boot and use @Autowired for injecting dependencies into your components then Cover creates tests that inject these dependencies using @MockBean. This works for Repositories as well as Services and other Components that your Services and Controllers depend on.