У меня есть служба, которая проверяет, существует ли продукт. Возвращает true, если найдено, иначе false. Но когда я создаю класс модульного теста и вызываю метод, он всегда возвращает false, даже если продукт существует. Когда я запускаю приложение и тестирую его с помощью почтальона, оно работает как положено, возвращая true, если найдено, иначе false.
Класс обслуживания (работает, как и ожидалось, когда я использую api с использованием почтальона через контроллер)
@Service
@Transactional
public class ProductService {
private ProductRepository productRepository;
@Autowired
public ProductService(ProductRepository productRepository) {this.productRepository = productRepository;}
//check if a product exists
public boolean checkIfProductExists(String name)
{
Product product = productRepository.findByName(name);
if(product != null)
{
return true;
}
return false;
}
}
Тест
@RunWith(SpringRunner.class)
public class ProductServiceTests {
@MockBean
private ProductService productService;
@Test
public void checkIfProductExistsTest()
{
//result is supposed to be true because ARVs exists
//when i consume api using postman it returns true, but here false
//why is it always false no matter the input
boolean result = productService.checkIfProductExists("ARVs");
//therefore test fails but it is supposed to pass
assertEquals(true,result);
}
}