![](/img/trans.png)
[英]junit test cases for SpringBoot application which uses non JPA Repository object
[英]How to call real methods of jpa repository in Junit5 mockito test cases for springboot services
我有下面的存储库接口,它正在我的服务类中使用。
@Repository
public interface InventoryRepository extends JpaRepository<Inventory, Integer> {
Inventory findByInventoryIdAndCompanyId(Integer inventoryId, Integer companyId);
}
在我的服务测试类中,我有这两个依赖项
@InjectMocks
InventoryService inventoryService;
@Mock
InventoryRepository inventoryRepository;
另外,下面是我在同一个类中的测试方法
@Test
public void getInventoryByIdTest() {
when(inventoryRepository.findByInventoryIdAndCompanyId(16591,1)).thenCallRealMethod();
Assertions.assertEquals(16591,inventoryService.getInventoryById(16591 , 1).getInventoryId());
}
我正在尝试为服务编写测试用例,该服务在内部调用 JPA 存储库方法以从 DB 获取数据。 我想调用 JPA 存储库的真实方法,我想获得真实的 db 结果而不是模拟它。 但它不起作用并抛出以下错误。
org.mockito.exceptions.base.MockitoException:
Cannot call abstract real method on java object!
Calling real methods is only possible when mocking non abstract method.
//correct example:
when(mockOfConcreteClass.nonAbstractMethod()).thenCallRealMethod();
有没有可能的方法来解决它?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.