如何模拟EntityManager?

发布于 2024-10-04 09:41:25 字数 1805 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我最亲爱的 2024-10-11 09:41:25

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

@Mock
private EntityManager entityManager; 

如果你想使用属于entityManager的任何方法,你应该调用。

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

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

请在此处阅读完整文档。

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

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

@Mock
private 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);

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

寂寞陪衬 2024-10-11 09:41:25

对于模拟,我建议使用 powermock。多亏了自动生成的代理,它几乎可以做你能想象到的任何事情,从从接口创建模拟开始,通过拦截初始化,最后抑制静态初始化(唯一让我困惑的是模拟 java.lang.Object< /代码>)。

假设 SessionFacadeTestSeesionFacade 的 JUnit 测试套件。

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();
    }
}

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

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

Let's say the SessionFacadeTest is your JUnit test suite for 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).

栖竹 2024-10-11 09:41:25

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文