Answer the question
In order to leave comments, you need to log in
Should a plugin be trusted to write unit tests?
While studying testing, I came across a plugin
Diff bluewho writes the tests.
@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();
}
}
Answer the question
In order to leave comments, you need to log in
It is worth adhering to the rule not to trust more than one automatic tool until you understand how it works under the hood and what tests it generates. In this case, the generation of tests itself is a useless exercise, since you don’t even know what exactly this code is testing and how.
I don't think it makes sense, since the essence of the test is to check the correctness of the code. These tests are generated so that they pass with the current code. So they are only suitable as protection against changes in behavior during refactoring
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question