如何模拟EntityManager? [英] How to mock EntityManager?

查看:87
本文介绍了如何模拟EntityManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要模拟实体管理器以使测试服务层(在我的情况下为会话外观)独立于基础层(在我的情况下为实体管理器).

I need to mock entity-manager to make testing service layer (in my case a session facade) to be independent of the underlying layer (which in my case is the entity-manager).

那么我该如何完成呢?我应该使用dbunit吗?我需要easy/j(模拟)吗?

So how I can accomplish this? Should I use dbunit? Do I need easy/j(Mock)?

推荐答案

我建议使用Mockito Framework,它非常易于使用和理解.

I suggest to use Mockito Framework it is very easy to use and understand.

@Mock
private EntityManager entityManager; 

如果要使用任何属于entityManager的方法,则应调用.

If you want to use any method that belongs to entityManager, you should call.

Mockito.when(METHOD_EXPECTED_TO_BE_CALLED).thenReturn(AnyObjectoftheReturnType);

运行测试时,在Mockito中预先声明的任何调用.当对EntityManager进行调用时,都会返回放置在声明中的值.

When you run your test, any call previosly declared in the Mockito.when for the EntityManager will return the value put in the declaration..

在此处阅读完整的文档.

Read full documentation here.

https://static.javadoc.io/org.mockito/mockito-core/2.12.0/org/mockito/Mockito.html#stubbing

这篇关于如何模拟EntityManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆