<legend id='Fpgy1'><style id='Fpgy1'><dir id='Fpgy1'><q id='Fpgy1'></q></dir></style></legend>

<small id='Fpgy1'></small><noframes id='Fpgy1'>

    <i id='Fpgy1'><tr id='Fpgy1'><dt id='Fpgy1'><q id='Fpgy1'><span id='Fpgy1'><b id='Fpgy1'><form id='Fpgy1'><ins id='Fpgy1'></ins><ul id='Fpgy1'></ul><sub id='Fpgy1'></sub></form><legend id='Fpgy1'></legend><bdo id='Fpgy1'><pre id='Fpgy1'><center id='Fpgy1'></center></pre></bdo></b><th id='Fpgy1'></th></span></q></dt></tr></i><div id='Fpgy1'><tfoot id='Fpgy1'></tfoot><dl id='Fpgy1'><fieldset id='Fpgy1'></fieldset></dl></div>

    1. <tfoot id='Fpgy1'></tfoot>
        <bdo id='Fpgy1'></bdo><ul id='Fpgy1'></ul>
    2. 如何使用 Mockito 和 jUnit 模拟持久化和实体

      时间:2023-10-01
      <tfoot id='IAgXE'></tfoot>

        • <i id='IAgXE'><tr id='IAgXE'><dt id='IAgXE'><q id='IAgXE'><span id='IAgXE'><b id='IAgXE'><form id='IAgXE'><ins id='IAgXE'></ins><ul id='IAgXE'></ul><sub id='IAgXE'></sub></form><legend id='IAgXE'></legend><bdo id='IAgXE'><pre id='IAgXE'><center id='IAgXE'></center></pre></bdo></b><th id='IAgXE'></th></span></q></dt></tr></i><div id='IAgXE'><tfoot id='IAgXE'></tfoot><dl id='IAgXE'><fieldset id='IAgXE'></fieldset></dl></div>
            <bdo id='IAgXE'></bdo><ul id='IAgXE'></ul>
          • <small id='IAgXE'></small><noframes id='IAgXE'>

              <legend id='IAgXE'><style id='IAgXE'><dir id='IAgXE'><q id='IAgXE'></q></dir></style></legend>
                  <tbody id='IAgXE'></tbody>

                本文介绍了如何使用 Mockito 和 jUnit 模拟持久化和实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试寻找一种方法来使用 Mockito 测试我的实体;

                I'm trying to find a way to test my entity using Mockito;

                这是简单的测试方法:

                @Mock
                private EntityManager em;
                
                @Test
                public void persistArticleWithValidArticleSetsArticleId() {
                    Article article = new Article();
                    em.persist(article);
                    assertThat(article.getId(), is(not(0L)));
                }
                

                如何最好地模拟 EntityManager 将 Id 从 0L 更改为即 1L 的行为?可能在可读性方面的障碍最少.

                How do I best mock the behaviour that the EntityManager changes the Id from 0L to i.e. 1L? Possibly with the least obstructions in readability.

                一些额外的信息;在测试范围之外,EntityManager 由应用程序容器生成

                Some extra information; Outside test-scope the EntityManager is produced by an application-container

                推荐答案

                public class AssignIdToArticleAnswer implements Answer<Void> {
                
                    private final Long id;
                
                    public AssignIdToArticleAnswer(Long id) {
                        this.id = id;
                    }
                
                    @Override
                    public Void answer(InvocationOnMock invocation) throws Throwable {
                        Article article = (Article) invocation.getArguments()[0];
                        article.setId(id);
                        return null;
                    }
                }
                

                然后

                doAnswer(new AssignIdToArticleAnswer(1L)).when(em).persist(any(Article.class));
                

                这篇关于如何使用 Mockito 和 jUnit 模拟持久化和实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:测试方法之外的 Mockito 存根 下一篇:Mockito + Spring + @PostConstruct,mock初始化错误,为什

                相关文章

                最新文章

                1. <legend id='QAmJs'><style id='QAmJs'><dir id='QAmJs'><q id='QAmJs'></q></dir></style></legend>

                    <tfoot id='QAmJs'></tfoot>
                  1. <i id='QAmJs'><tr id='QAmJs'><dt id='QAmJs'><q id='QAmJs'><span id='QAmJs'><b id='QAmJs'><form id='QAmJs'><ins id='QAmJs'></ins><ul id='QAmJs'></ul><sub id='QAmJs'></sub></form><legend id='QAmJs'></legend><bdo id='QAmJs'><pre id='QAmJs'><center id='QAmJs'></center></pre></bdo></b><th id='QAmJs'></th></span></q></dt></tr></i><div id='QAmJs'><tfoot id='QAmJs'></tfoot><dl id='QAmJs'><fieldset id='QAmJs'></fieldset></dl></div>

                    <small id='QAmJs'></small><noframes id='QAmJs'>

                      <bdo id='QAmJs'></bdo><ul id='QAmJs'></ul>