Java 如何模拟 EntityManager?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4296042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:29:41  来源:igfitidea点击:

How to mock EntityManager?

javaunit-testingjpamockingentitymanager

提问by Muhammad Hewedy

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).

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

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

那么我如何才能做到这一点?我应该使用 dbunit 吗?我需要easy/j(Mock)吗?

采纳答案by Koitoer

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

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

@Mock
private EntityManager entityManager; 

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

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

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

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

当您运行测试时,之前在 Mockito.when 中为 EntityManager 声明的任何调用都将返回放入声明中的值。

Read full documentation here.

在此处阅读完整文档。

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

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

回答by Przemek Kryger

For mocking, I'd suggest powermock. Thanks to auto generated proxies, it can do virtually anything you can imagine, starting with creating mocks from interfaces, through intercepting initialization finishing with suppressing static initialization (the only thing that beat me was messing with mocking java.lang.Object).

对于嘲笑,我建议使用powermock。多亏了自动生成的代理,它几乎可以做任何你能想象到的事情,从从接口创建模拟开始,到拦截初始化,最后是抑制静态初始化(唯一打败我的就是弄乱了 mocking java.lang.Object)。

Let's say the SessionFacadeTestis your JUnit test suite for SeesionFacade.

假设这SessionFacadeTest是您的 JUnit 测试套件SeesionFacade

import static org.powermock.api.easymock.PowerMock.createMock;
import static org.powermock.api.easymock.PowerMock.replayAll;
import static org.powermock.api.easymock.PowerMock.verifyAll;
import static org.easymock.EasyMock.anyObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import javax.persistence.EntityManager;

@RunWith(PowerMockRunner.class)
@PrepareForTest({SessionFacade.class})
public class SessionFacadeTest {
    @Test public void persistingObject() {
        //set up stage
        SessionFacade fixture = new SessionFacade();
        EntityManager managerMock = createMock(EntityManager.class);
        fixture.setManager(managerMock);
        //record expected behavior
        managerMock.persist(anyObject());
        //testing stage
        replayAll();
        fixture.anyMethodThatCallPersist();
        //asserting stage
        verifyAll();
    }
}

(Note: I wrote it here, so may even not compile, but shall give you the idea).

(注意:我在这里写的,所以甚至可能不会编译,但会给你这个想法)。

回答by Stefan Haberl

I'm usually using EasyMock for mocking concrete service implementation in test cases. Check out their user guide. It includes a a very easy to follow step-by-step guide, which explains the basic concepts behind mocking frameworks in general and gets you up and running with EasyMock fast.

我通常使用 EasyMock 来模拟测试用例中的具体服务实现。查看他们的用户指南。它包括一个非常易于遵循的分步指南,它总体上解释了模拟框架背后的基本概念,并让您快速启动和运行 EasyMock。