src/test/java/com/sapient/XYZecommmarket/services/ProductServiceTests.java
@Test
    public void testGetProductsByBrand() {
        List<Product> prodListForBrand = new ArrayList<Product>();
        Product prod1 = new Product();
        prod1.setId(3L);
        prod1.setName("Black Cap");
        prod1.setBrand(new Brand(3L, "Nike"));
        prod1.setType(new ProductType(3L, "cap"));
        prod1.setPrice(500.0);
        prod1.setColor("black");
        prod1.setSize(16);
        prod1.setAvailableCount(600);
        prod1.setSupplier(new Supplier(3L, "AbcRetail"));
        prodListForBrand.add(prod1);
        when(dataServiceMock.findAllByBrandName(anyString(), any(PageRequest.class))).thenReturn(new PageImpl<>(prodListForBrand));
        assertTrue(productService.getProductsByBrand("Nike", 1).hasContent());
        assertEquals(1, productService.getProductsByBrand("Nike", 1).getTotalElements());
        assertEquals("Black Cap", productService.getProductsByBrand("Nike", 1).getContent().get(0).getName());
    }
/**
     * business layer test with mock DB data
     */
business layer test with mock DB data
business layer test with mock DB data
@Test
    public void testGetProductsByBrand() {
        List<Product> prodListForBrand = new ArrayList<Product>();
        Product prod1 = new Product();
        prod1.setId(3L);
        prod1.setName("Black Cap");
        prod1.setBrand(new Brand(3L, "Nike"));
        prod1.setType(new ProductType(3L, "cap"));
        prod1.setPrice(500.0);
        prod1.setColor("black");
        prod1.setSize(16);
        prod1.setAvailableCount(600);
        prod1.setSupplier(new Supplier(3L, "AbcRetail"));
        prodListForBrand.add(prod1);
        when(dataServiceMock.findAllByBrandName(anyString(), any(PageRequest.class))).thenReturn(new PageImpl<>(prodListForBrand));
        assertTrue(productService.getProductsByBrand("Nike", 1).hasContent());
        assertEquals(1, productService.getProductsByBrand("Nike", 1).getTotalElements());
        assertEquals("Black Cap", productService.getProductsByBrand("Nike", 1).getContent().get(0).getName());
    }
@ Test public void testGetProductsByBrand ( ) { List < Product > prodListForBrand = new ArrayList < Product > ( ) ; Product prod1 = new Product ( ) ; prod1 . setId ( 3L ) ; prod1 . setName ( "Black Cap" ) ; prod1 . setBrand ( new Brand ( 3L , "Nike" ) ) ; prod1 . setType ( new ProductType ( 3L , "cap" ) ) ; prod1 . setPrice ( 500.0 ) ; prod1 . setColor ( "black" ) ; prod1 . setSize ( 16 ) ; prod1 . setAvailableCount ( 600 ) ; prod1 . setSupplier ( new Supplier ( 3L , "AbcRetail" ) ) ; prodListForBrand . add ( prod1 ) ; when ( dataServiceMock . findAllByBrandName ( anyString ( ) , any ( PageRequest . class ) ) ) . thenReturn ( new PageImpl < > ( prodListForBrand ) ) ; assertTrue ( productService . getProductsByBrand ( "Nike" , 1 ) . hasContent ( ) ) ; assertEquals ( 1 , productService . getProductsByBrand ( "Nike" , 1 ) . getTotalElements ( ) ) ; assertEquals ( "Black Cap" , productService . getProductsByBrand ( "Nike" , 1 ) . getContent ( ) . get ( 0 ) . getName ( ) ) ; }
business layer test with mock DB data
business layer test with mock DB data