pipilam
pipilam

Reputation: 587

Unit test throws exception

I am trying to unit test method that is responsible for returning the number of fat of given product.

public class NutrientsCalculationService {

    ....

    double countFatNumberOfGivenProduct(UserProduct productToCalculate) {
        double fatNumber = retrieveGivenProductFromDB(productToCalculate).getFatNumber(); // All given data in DB are per 100g!!!
        return (fatNumber * productToCalculate.getGram()) / ONE_HUNDRED_GRAMS;
    }


    Product retrieveGivenProductFromDB(UserProduct productToCalculate) {
        if (productToCalculate.getGram() > 0) {
            return productRepository.findByName(productToCalculate.getName())
                    .orElseThrow(() -> new IllegalArgumentException("Product does not exist!"));
        } else {
            throw new IllegalArgumentException("Grams can not be negative");
        }
    }

I tried to write a unit test for it, but it throws an exception saying that product does not exist. What should I change in this test?

@RunWith(MockitoJUnitRunner.class)
public class NutrientsCalculationServiceTest {

    @Mock
    private ProductRepository productRepository;
    @Mock
    private AccountRepository accountRepository;

    @InjectMocks
    private NutrientsCalculationService nutrientsCalculationService;

    @Test
    public void countFatNumberOfGivenProduct() {
        UserProduct userProduct = createDummyUserProduct();
        Product product = createDummyProduct();
        //when(productRepository.findByName(userProduct.getName())).thenReturn(product);
        //when(nutrientsCalculationService.retrieveGivenProductFromDB(userProduct)).thenReturn(product);
        double expectedFatNumber = nutrientsCalculationService.countFatNumberOfGivenProduct(userProduct);
        double actualFatNumber = product.getFatNumber();

        assertEquals(expectedFatNumber, actualFatNumber,0.0002);
    }

    private UserProduct createDummyUserProduct() {
        return new UserProduct(1, "Schnitzel", 129, 4.2);
    }

    private Product createDummyProduct() {
        return new Product.ProductBuilder()
                .withName("Schnitzel")
                .withCarbohydratesNumber(0)
                .withFatNumber(4.2)
                .withProteinsNumber(22.9)
                .withKcal(129)
                .withType(ProductType.MEAT)
                .build();
    }
}

java.lang.IllegalArgumentException: Product does not exist!

at trainingapp.calculations.NutrientsCalculationService.lambda$retrieveGivenProductFromDB$0(NutrientsCalculationService.java:51)
at java.base/java.util.Optional.orElseThrow(Optional.java:397)
at trainingapp.calculations.NutrientsCalculationService.retrieveGivenProductFromDB(NutrientsCalculationService.java:51)
at trainingapp.calculations.NutrientsCalculationService.countFatNumberOfGivenProduct(NutrientsCalculationService.java:29)
at trainingapp.calculations.NutrientsCalculationServiceTest.countFatNumberOfGivenProduct(NutrientsCalculationServiceTest.java:34)

Upvotes: 0

Views: 532

Answers (1)

Dominik Wosiński
Dominik Wosiński

Reputation: 3874

In Your example You have created Mock of the ProductRepository but You haven't really said what should happen when you call methods on the ProductRepository. EDIT: I've just noticed that this part is commented, if You uncomment it - it should work fine.

Upvotes: 2

Related Questions