Commit 5a6c6cec authored by vikram singh's avatar vikram singh

Added junit and component testcases for the item and orders

parent 8890135c
package com.ecom.controller;
import com.ecom.config.TestConfig;
import com.ecom.model.Item;
import com.ecom.repository.ItemRepository;
import com.ecom.service.impl.ItemServiceImpl;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentMatchers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
@SpringBootTest(classes = {ItemController.class})
public class ItemControllerJunitTest {
@Autowired
private ItemController itemController;
@MockBean
private ItemServiceImpl itemServiceImpl;
@DisplayName("This method will test the save method it will mock db ")
@ParameterizedTest
@MethodSource("getItemSource")
public void saveTest(Item item){
when(itemServiceImpl.save(ArgumentMatchers.any(Item.class))).thenReturn(item);
ResponseEntity<Item> responseEntity = itemController.save(item);
assertAll( "savetest",
()-> assertEquals(responseEntity.getStatusCode(), HttpStatus.CREATED,()->"status should be 201")
,()->assertEquals(responseEntity.getBody().getId(),1,()->"id should be mocked id only")
);
}
@DisplayName("This method will test the save method it will mock db ")
@ParameterizedTest
@MethodSource("getItemSource")
public void saveNegativeTest(Item item){
when(itemServiceImpl.save(ArgumentMatchers.any(Item.class))).thenReturn(null);
ResponseEntity<Item> responseEntity = itemController.save(item);
assertAll( "savetestnegative",
()-> assertEquals(responseEntity.getStatusCode(), HttpStatus.INTERNAL_SERVER_ERROR,()->"status should be 500")
,()->assertNull(responseEntity.getBody(),()->"body is null")
);
}
@DisplayName("This method will test the get method it will mock db ")
@ParameterizedTest
@ValueSource(ints={1})
public void getByIdTest(int id){
Item response=mockItem();
when(itemServiceImpl.findById(ArgumentMatchers.anyInt())).thenReturn(Optional.of(response));
ResponseEntity<Optional<Item>> responseEntity = itemController.getById(id);
assertAll( "gettest",
()-> assertEquals(responseEntity.getStatusCode(), HttpStatus.OK,()->"status should be 200")
,()->assertNotNull(responseEntity.getBody(),()->"body is not null")
);
}
@DisplayName("This method will test the get method for negative scenario it will mock db ")
@ParameterizedTest
@ValueSource(ints={1})
public void getByIdTestNegative(int id){
Item response=mockItem();
when(itemServiceImpl.findById(ArgumentMatchers.anyInt())).thenReturn(Optional.ofNullable(null));
ResponseEntity<Optional<Item>> responseEntity = itemController.getById(id);
assertAll( "gettestnegative",
()-> assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND,()->"status should be NotFound")
,()->assertEquals(responseEntity.getBody(),Optional.empty(),()->"body is null")
);
}
@DisplayName("This method will test the getAll method it will mock db ")
@Test
public void getAllTest(){
List<Item> response=Arrays.asList(mockItem());
when(itemServiceImpl.findAll()).thenReturn(response);
ResponseEntity<List<Item>> responseEntity = itemController.getAll();
assertAll( "getAllTest",
()-> assertEquals(responseEntity.getStatusCode(), HttpStatus.OK,()->"status should be 200")
,()->assertEquals(responseEntity.getBody(),response,()->"body is List")
);
}
@DisplayName("This method will test the getAll method for Negeate test it will mock db ")
@Test
public void getAllTestNegative(){
List<Item> response=Arrays.asList(mockItem());
when(itemServiceImpl.findAll()).thenReturn(null);
ResponseEntity<List<Item>> responseEntity = itemController.getAll();
assertAll( "getAllTest",
()-> assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND,()->"status should be Not found")
,()->assertNull(responseEntity.getBody(),()->"body is null")
);
}
private static Stream<Arguments> getItemSource(){
return Stream.of(Arguments.of(mockItem()));
}
public static Item mockItem() {
Item item=new Item();
item.setQty(1);
item.setId(1);
item.setName("test");
item.setPrice(1);
return item;
}
}
package com.ecom.service;
import com.ecom.model.Item;
import com.ecom.repository.ItemRepository;
import com.ecom.service.impl.ItemServiceImpl;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentMatchers;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
@SpringBootTest(classes = {ItemServiceImpl.class})
@ExtendWith(SpringExtension.class) //takes care of all the spring annotation test
@ExtendWith(MockitoExtension.class) // checks redundent mocking // also no need to add Mockito.initMock(this);
public class ItemServiceImplTest {
@Autowired
private ItemServiceImpl itemServiceImpl;
@MockBean
private ItemRepository itemRepository;
@DisplayName("This method will test the save method it will mock db ")
@ParameterizedTest
@MethodSource("getItemSource")
public void saveTest(Item item){
when(itemRepository.save(ArgumentMatchers.any(Item.class))).thenReturn(item);
Item itemResponse = itemServiceImpl.save(item);
assertAll( "savetest",
()-> assertEquals(itemResponse.getId(), item.getId(),()->"item id should be matched"),
()-> assertEquals(itemResponse.getName(), item.getName(),()->"item name should be matched")
);
}
private static Stream<Arguments> getItemSource(){
return Stream.of(Arguments.of(mockItem()));
}
public static Item mockItem() {
Item item=new Item();
item.setQty(1);
item.setId(1);
item.setName("test");
item.setPrice(1);
return item;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment