При изучении тестирование наткнулся на плагин
Diffblue
, который пишет тесты.
Сам в тестировании пока-что ничего не понимаю, чтобы оценить результат. Прошу оценить работу
результат работы плагина.
Обычный сервис класс:
@Service
@Slf4j
public class ProductService {
@Autowired
private ProductRepository repo;
public List<Product> listAll(String keyword) {
if (keyword != null) {
log.info("Поиск продукта по ключевому слову {}", keyword);
return repo.search(keyword);
}
return repo.findAll();
}
public void save(Product product) {
if (product != null){
repo.save(product);
}else {
log.error("Продукт не может быть пустым!");
}
}
public Product get(Long id) {
if (repo.findById(id).isPresent()){
return repo.findById(id).get();
}
log.error("Продукт не найден");
return null;
}
public void delete(Long id) {
log.warn("Продукт по id {} удален", id);
repo.deleteById(id);
}
public List<Product> getAll(){
return repo.findAll();
}
}
тест:
@ContextConfiguration(classes = {ProductService.class})
@ExtendWith(SpringExtension.class)
class ProductServiceTest {
@MockBean
private ProductRepository productRepository;
@Autowired
private ProductService productService;
@Test
void testListAll() {
ArrayList<Product> productList = new ArrayList<Product>();
when(this.productRepository.search((String) any())).thenReturn(productList);
List<Product> actualListAllResult = this.productService.listAll("Keyword");
assertSame(productList, actualListAllResult);
assertTrue(actualListAllResult.isEmpty());
verify(this.productRepository).search((String) any());
assertTrue(this.productService.getAll().isEmpty());
}
@Test
void testSave() {
Product product = new Product();
product.setPrice(10.0);
product.setId(123L);
product.setName("Name");
product.setCategoryList(new ArrayList<Category>());
product.setDescription("The characteristics of someone or something");
product.setCategory2("Category2");
when(this.productRepository.save((Product) any())).thenReturn(product);
Product product1 = new Product();
product1.setPrice(10.0);
product1.setId(123L);
product1.setName("Name");
product1.setCategoryList(new ArrayList<Category>());
product1.setDescription("The characteristics of someone or something");
product1.setCategory2("Category2");
this.productService.save(product1);
verify(this.productRepository).save((Product) any());
assertTrue(this.productService.getAll().isEmpty());
}
@Test
void testGet() {
Product product = new Product();
product.setPrice(10.0);
product.setId(123L);
product.setName("Name");
product.setCategoryList(new ArrayList<Category>());
product.setDescription("The characteristics of someone or something");
product.setCategory2("Category2");
Optional<Product> ofResult = Optional.<Product>of(product);
when(this.productRepository.findById((Long) any())).thenReturn(ofResult);
assertSame(product, this.productService.get(123L));
verify(this.productRepository, atLeast(1)).findById((Long) any());
assertTrue(this.productService.getAll().isEmpty());
}
@Test
void testDelete() {
doNothing().when(this.productRepository).deleteById((Long) any());
this.productService.delete(123L);
verify(this.productRepository).deleteById((Long) any());
assertTrue(this.productService.getAll().isEmpty());
}
@Test
void testGetAll() {
ArrayList<Product> productList = new ArrayList<Product>();
when(this.productRepository.findAll()).thenReturn(productList);
List<Product> actualAll = this.productService.getAll();
assertSame(productList, actualAll);
assertTrue(actualAll.isEmpty());
verify(this.productRepository).findAll();
}
}