lumi
lumi

Reputation: 1

AssertEquals failing . List size is returning more than actual size

everyone. I'm new to Spring and unit testing and I'm trying to test my repository layer. My test is failing when using assertEquals when asserting the size of my products list. In the first test, it says actual is 3 when clearly it should be 2.Same goes for the second test, it's returning 2 when it should be 1 after deletion of product1.I don't know what I'm doing wrong.

Thank you in advance.

import com.eshop.shoppingcart.model.Product;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;


import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

import static org.junit.jupiter.api.Assertions.*;


@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class ProductRepositoryTest {
    @Autowired
   ProductRepository productRepository;

    @Test
    void getAllProducts(){
        Product product1 = new Product();
        product1.setId(1L);
        product1.setName("The Stranger");
        product1.setPrice(124.5);
        product1.setPictureUrl("https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1590930002l/49552._SY475_.jpg");

        Product product2 = new Product();
        product2.setId(2L);
        product2.setName("Meditations");
        product2.setPrice(164.5);
        product2.setPictureUrl("https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1590930002l/49552._SY475_.jpg");

        productRepository.save(product1);
        productRepository.save(product2);

        List<Product> products = productRepository.findAll();

        assertNotNull(products);
        assertThat(products).isNotNull();
        assertThat(products.size()).isEqualTo(2);
        assertEquals(2, products.size());

    }

    @Test
    void deleteProduct(){
        Product product1 = new Product();
        product1.setId(1L);
        product1.setName("The Stranger");
        product1.setPrice(124.5);
        product1.setPictureUrl("https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1590930002l/49552._SY475_.jpg");
        productRepository.save(product1);
        Long id = product1.getId();

        Product product2 = new Product();
        product2.setId(2L);
        product2.setName("Meditations");
        product2.setPrice(164.5);
        product2.setPictureUrl("https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1590930002l/49552._SY475_.jpg");
        productRepository.save(product2);

        productRepository.delete(product1);
        List<Product> products = productRepository.findAll();
        Optional<Product> retrievedProduct =  productRepository.findById(id);

        assertThat(retrievedProduct.isEmpty());
        assertEquals(1, products.size());

    }
}

Upvotes: 0

Views: 194

Answers (1)

Alberto Mart&#237;n
Alberto Mart&#237;n

Reputation: 119

You may add some code to clean the repository data.

It seems that you already have a product when the test starts.

Upvotes: 2

Related Questions