单元测试如何使用Mockito模拟存储库

编程入门 行业动态 更新时间:2024-10-25 02:27:50
本文介绍了单元测试如何使用Mockito模拟存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在清除存储库时遇到问题。有人建议我只创建另一个Applation.Properties(我还没有这样做),并使用像h2这样的内存数据库。不过,我想知道是否可以只将调用存根,这样在调用myDataService.findById(Id)时,而不是试图从数据库中获取时,只会返回一个模拟对象?

我刚开始为我的单元测试和Spring Boot编写模拟代码,所以我可能遗漏了一些东西。下面的代码(试图简化名称并使其成为通用名称以便在此处发布)。

我的测试类

public class MyServiceImplTest { private MyDataService myDataService; private NyService myService; private MyRepository myRepository; @Before public void setUp() { myDataService = Mockito.mock(MyDataServiceImpl.class); myService = new MyServiceImpl(myDataService); } @Test public void getById_ValidId() { doReturn(MyMockData.getMyObject()).when(myDataService).findById("1"); when(myService.getById("1")).thenReturn(MyMockData.getMyObject()); MyObject myObject = myService.getById("1"); //Whatever asserts need to be done on the object myObject } }

用于对数据层进行服务调用的类

@Service public class MyServiceImpl implements MyService { MyDataService myDataService; @Autowired public MyServiceImpl(MyDataService myDataService) { this.myDataService = myDataService; } @Override public MyObject getById(String id) { if(id == null || id == "") { throw new InvalidRequestException("Invalid Identifier"); } MyObject myObj; try { myObj = myDataService.findById(id); }catch(Exception ex) { throw new SystemException("Internal Server Error"); } return myObj; } }

这就是我在测试中遇到问题的地方。当调用findById()方法时,变量存储库为空,因此当尝试执行repository.findOne(Id)时,它抛出异常n。这就是我尝试模拟的内容,但存储库给我带来了问题。

@Repository @Qualifier("MyRepo") public class MyDataServiceImpl { @PersistenceContext private EntityManager em; private MyRepository repository; @Autowired public MyDataServiceImpl(MyRepository repository) { super(repository); this.repository = repository; } public MyObject findById(String id) { P persitentObject = repository.findOne(id); //Calls to map what persitentObject holds to MyObject and returns a MyObject } }

此处MyRepository的代码只是为了显示它是一个扩展CrudRepository的空接口

public interface MyRepository extends CrudRepository<MyObjectPO, String>, JpaSpecificationExecutor<MyObjectPO> { } 推荐答案

首先我要说的是,通过使用构造函数注入而不是字段注入(这使得使用模拟编写测试要简单得多),您走上了正确的道路。

public class MyServiceImplTest { private MyDataService myDataService; private NyService myService; @Mock private MyRepository myRepository; @Before public void setUp() { MockitoAnnotations.initMocks(this); // this is needed for inititalizytion of mocks, if you use @Mock myDataService = new MyDataServiceImpl(myRepository); myService = new MyServiceImpl(myDataService); } @Test public void getById_ValidId() { doReturn(someMockData).when(myRepository).findOne("1"); MyObject myObject = myService.getById("1"); //Whatever asserts need to be done on the object myObject } } 从您的服务-->dataService-->开始调用。但只模拟您的存储库调用。 通过这种方式,您可以控制和测试类的所有其他部分(包括服务和数据服务),并且只模拟存储库调用。

更多推荐

单元测试如何使用Mockito模拟存储库

本文发布于:2023-06-13 09:31:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/678949.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   单元测试   Mockito

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!