[英]How to write Junit test cases for EntityManger createNativeQuery with String and class as parameters
@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;
}
}
I have the above class to get records from DB.我有上面的 class 从数据库中获取记录。 I wrote JUnit test for this above class as
我为上面的 class 写了 JUnit 测试为
@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));
}
}
I have problem mocking and at this em.createNativeQuery(queryString,Designator.class)
in dao class I am getting null
.我有问题 mocking 和这个
em.createNativeQuery(queryString,Designator.class)
在道 class 我得到null
。 How can I properly mock the em.createNativeQuery
in test class?如何在测试 class 中正确模拟
em.createNativeQuery
?
According to the documentation, if you are using argument matchers, all arguments have to be provided by matchers .根据文档,如果您使用参数匹配器,则所有 arguments 都必须由 matchers 提供。 Hence, try using
ArgumentMatchers.eq()
( reference documentation ) to check for Designator.class
equality:因此,尝试使用
ArgumentMatchers.eq()
( 参考文档)来检查Designator.class
是否相等:
@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;
}
}
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:然后您需要删除
@InjectMocks
因为您在初始化之前注入它们并创建DesignatorDaoImpl
的实例以在@Before
方法中进行测试:
@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));
}
}
If you can't change DesignatorDaoImpl
then use @RunWith(MockitoJUnitRunner.class)
instead of MockitoAnnotations.initMocks(this);
如果你不能改变
DesignatorDaoImpl
然后使用@RunWith(MockitoJUnitRunner.class)
而不是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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.