• <legend id='uqrIF'><style id='uqrIF'><dir id='uqrIF'><q id='uqrIF'></q></dir></style></legend>

      • <bdo id='uqrIF'></bdo><ul id='uqrIF'></ul>

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

        <tfoot id='uqrIF'></tfoot>
      1. <i id='uqrIF'><tr id='uqrIF'><dt id='uqrIF'><q id='uqrIF'><span id='uqrIF'><b id='uqrIF'><form id='uqrIF'><ins id='uqrIF'></ins><ul id='uqrIF'></ul><sub id='uqrIF'></sub></form><legend id='uqrIF'></legend><bdo id='uqrIF'><pre id='uqrIF'><center id='uqrIF'></center></pre></bdo></b><th id='uqrIF'></th></span></q></dt></tr></i><div id='uqrIF'><tfoot id='uqrIF'></tfoot><dl id='uqrIF'><fieldset id='uqrIF'></fieldset></dl></div>
      2. 如何使用 String 和 class 作为参数为 EntityManger cr

        时间:2023-09-26
      3. <tfoot id='XMJad'></tfoot>
      4. <i id='XMJad'><tr id='XMJad'><dt id='XMJad'><q id='XMJad'><span id='XMJad'><b id='XMJad'><form id='XMJad'><ins id='XMJad'></ins><ul id='XMJad'></ul><sub id='XMJad'></sub></form><legend id='XMJad'></legend><bdo id='XMJad'><pre id='XMJad'><center id='XMJad'></center></pre></bdo></b><th id='XMJad'></th></span></q></dt></tr></i><div id='XMJad'><tfoot id='XMJad'></tfoot><dl id='XMJad'><fieldset id='XMJad'></fieldset></dl></div>

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

              <tbody id='XMJad'></tbody>
            <legend id='XMJad'><style id='XMJad'><dir id='XMJad'><q id='XMJad'></q></dir></style></legend>
              • <bdo id='XMJad'></bdo><ul id='XMJad'></ul>

                1. 本文介绍了如何使用 String 和 class 作为参数为 EntityManger createNativeQuery 编写 Junit 测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  @Repository
                  public class DesignatorDaoImpl extends GenericDaoImpl implements DesignatorDao {
                  
                      @Autowired
                      EntityManager em;
                  
                      public List<Designator> getDesignators(DesignatorRequestDTO dto) throws Exception {
                          List<Designator> designatorsList = new ArrayList<>();
                          int start = dto.getStart();
                          String queryString ="select * from ( SELECT * FROM TAB.DESIGNATOR WHERE ACTIVE_IND ='A' ORDER BY LAST_EDITED_DATE DESC ) OFFSET "+ start +" ROWS FETCH NEXT "+dto.getLimit()+" ROWS ONLY";          
                          Query query = em.createNativeQuery(queryString,Designator.class);
                          designatorsList = query.getResultList();
                          return designatorsList;
                      }
                  }
                  

                  我有上面的类来从数据库中获取记录.我为上面的这个类编写了 JUnit 测试

                  I have the above class to get records from DB. I wrote JUnit test for this above class as

                  @Transactional
                  @Rollback(true)
                  public class DesignatorDaoImplTest {
                  
                      @Mock
                      EntityManager entityManagerMock;
                  
                      @Mock
                      TypedQuery<Designator> getQuery;
                  
                      @InjectMocks
                      DesignatorDaoImpl designatorDaoImpl;
                  
                      DesignatorRequestDTO dto = new DesignatorRequestDTO();
                  
                      List<Designator> designatorEntityList = new ArrayList<>();
                  
                      @Before
                      public void setUp() throws Exception { 
                          MockitoAnnotations.initMocks(this);
                          dto.setLimit(4);
                          dto.setStart(1);
                  
                          Designator entity = new Designator();
                          entity.setId("dgbkk");
                          designatorEntityList.add(entity);
                      }
                  
                      @Test
                      public void saveDesignatorTest() throws Exception {
                          Mockito.when(entityManagerMock.createNamedQuery(ArgumentMatchers.anyString(), ArgumentMatchers.eq(Designator.class))).thenReturn(getQuery);
                          Mockito.when(getQuery.getResultList()).thenReturn(designatorEntityList);
                          assertNotNull(designatorDaoImpl.saveDesignators(dto));
                      }
                  }
                  
                  

                  我在模拟时遇到问题,在 dao 类中的这个 em.createNativeQuery(queryString,Designator.class) 中,我得到了 null.如何在测试类中正确模拟 em.createNativeQuery?

                  I have problem mocking and at this em.createNativeQuery(queryString,Designator.class) in dao class I am getting null. How can I properly mock the em.createNativeQuery in test class?

                  推荐答案

                  根据文档,如果你使用参数匹配器,所有参数都必须由匹配器提供.因此,请尝试使用 ArgumentMatchers.eq() (参考文档) 检查 Designator.class 是否相等:

                  According to the documentation, if you are using argument matchers, all arguments have to be provided by matchers. Hence, try using ArgumentMatchers.eq() (reference documentation) to check for Designator.class equality:

                  @Mock
                  TypedQuery<Designator> getQuery;
                  
                  @Test
                  public void saveDesignatorTest() throws Exception {
                      Mockito.when(entityManagerMock.createNamedQuery(ArgumentMatchers.anyString(), ArgumentMatchers.eq(Designator.class))).thenReturn(getQuery);
                      Mockito.when(getQuery.getResultList()).thenReturn(designatorEntityList);
                      assertNotNull(designatorDaoImpl.getDesignators(dto));
                  }
                  


                  避免使用属性注入,而是使用构造函数注入:


                  Avoid using property injection and use constructor injection instead:

                  @Repository
                  public class DesignatorDaoImpl extends GenericDaoImpl implements DesignatorDao {
                  
                      private EntityManager em;
                  
                      public DesignatorDaoImpl(EntityManager em) {
                          super();
                          this.em = em;
                      }
                  
                      public List<Designator> getDesignators(DesignatorRequestDTO dto) throws Exception {
                          List<Designator> designatorsList = new ArrayList<>();
                          int start = dto.getStart();
                          String queryString ="select * from ( SELECT * FROM TAB.DESIGNATOR WHERE ACTIVE_IND ='A' ORDER BY LAST_EDITED_DATE DESC ) OFFSET "+ start +" ROWS FETCH NEXT "+dto.getLimit()+" ROWS ONLY";          
                          Query query = em.createNativeQuery(queryString,Designator.class);
                          designatorsList = query.getResultList();
                          return designatorsList;
                      }
                  }
                  

                  然后您需要删除 @InjectMocks 因为您在初始化之前注入它们并创建 DesignatorDaoImpl 的实例以在 @Before 方法:

                  Then you need to drop @InjectMocks because you are injecting them before being initialized and create the instance of DesignatorDaoImpl to test in the @Before method:

                  @Transactional
                  @Rollback(true)
                  public class DesignatorDaoImplTest {
                  
                      @Mock
                      EntityManager entityManagerMock;
                  
                      @Mock
                      TypedQuery<Designator> getQuery;
                  
                      DesignatorDaoImpl designatorDaoImpl;
                  
                      DesignatorRequestDTO dto = new DesignatorRequestDTO();
                      List<Designator> designatorEntityList = new ArrayList<>();
                  
                      @Before
                      public void setUp() throws Exception { 
                          MockitoAnnotations.initMocks(this);
                          
                          dto.setLimit(4);
                          dto.setStart(1);
                  
                          Designator entity = new Designator();
                          entity.setId("dgbkk");
                          designatorEntityList.add(entity);
                  
                          designatorDaoImpl = new DesignatorDaoImpl(entityManagerMock);
                      }
                  
                      @Test
                      public void saveDesignatorTest() throws Exception {
                          Mockito.when(entityManagerMock.createNamedQuery(ArgumentMatchers.anyString(), ArgumentMatchers.eq(Designator.class))).thenReturn(getQuery);
                          Mockito.when(getQuery.getResultList()).thenReturn(designatorEntityList);
                          assertNotNull(designatorDaoImpl.saveDesignators(dto));
                      }
                  }
                  


                  如果你不能改变 DesignatorDaoImpl 然后使用 @RunWith(MockitoJUnitRunner.class) 而不是 MockitoAnnotations.initMocks(this);as如下:


                  If you can't change DesignatorDaoImpl then use @RunWith(MockitoJUnitRunner.class) instead of MockitoAnnotations.initMocks(this);as follows:

                  @Transactional
                  @Rollback(true)
                  @RunWith(MockitoJUnitRunner.class)
                  public class DesignatorDaoImplTest {
                  
                      @Mock
                      EntityManager entityManagerMock;
                  
                      @Mock
                      TypedQuery<Designator> getQuery;
                  
                      @InjectMocks
                      DesignatorDaoImpl designatorDaoImpl;
                  
                      DesignatorRequestDTO dto = new DesignatorRequestDTO();
                  
                      List<Designator> designatorEntityList = new ArrayList<>();
                  
                      @Before
                      public void setUp() throws Exception { 
                          MockitoAnnotations.initMocks(this);
                          dto.setLimit(4);
                          dto.setStart(1);
                  
                          Designator entity = new Designator();
                          entity.setId("dgbkk");
                          designatorEntityList.add(entity);
                      }
                  
                      @Test
                      public void saveDesignatorTest() throws Exception {
                          Mockito.when(entityManagerMock.createNamedQuery(ArgumentMatchers.anyString(), ArgumentMatchers.eq(Designator.class))).thenReturn(getQuery);
                          Mockito.when(getQuery.getResultList()).thenReturn(designatorEntityList);
                          assertNotNull(designatorDaoImpl.saveDesignators(dto));
                      }
                  }
                  

                  这篇关于如何使用 String 和 class 作为参数为 EntityManger createNativeQuery 编写 Junit 测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:模拟 ScheduledExecutorService 与“不要模拟你不拥有的 下一篇:Mockito 模拟 restTemplate.postForEntity

                  相关文章

                  最新文章

                2. <legend id='1oQ0a'><style id='1oQ0a'><dir id='1oQ0a'><q id='1oQ0a'></q></dir></style></legend>
                    <bdo id='1oQ0a'></bdo><ul id='1oQ0a'></ul>

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

                      <tfoot id='1oQ0a'></tfoot>

                      <small id='1oQ0a'></small><noframes id='1oQ0a'>