留言板
JUnit test case for Dynamic Query
I have a dynamic query like as given below
For the above code I have written a JUnit test cases like as shown below using mokito. I have followed this tutorial for achieving this
returnCollegeDetails() function
But I don't know how to cover the RestrictionsFactoryUtil, ProjectionFactoryUtil and OrderFactoryUtil.
Can anyone please tell me how to do this
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(College.class, getClassLoader());
dynamicQuery.add(RestrictionsFactoryUtil.eq("collegeName", "SUT"));
dynamicQuery.add(RestrictionsFactoryUtil.eq("collegePlace", "Chennai"));
dynamicQuery.setProjection(ProjectionFactoryUtil.projectionList()
.add(ProjectionFactoryUtil.groupProperty("collegeName"), "collegeName"));
dynamicQuery.addOrder(OrderFactoryUtil.asc("collegeName"));
try {
List<string> collegeDetails = (List<string>)CollegeLocalServiceUtil.dynamicQuery(dynamicQuery, start, end);
} catch (SystemException e) {
e.printStackTrace();
}
</string></string>
For the above code I have written a JUnit test cases like as shown below using mokito. I have followed this tutorial for achieving this
BeanLocator beanLocator = mock(BeanLocator.class);
PortalBeanLocatorUtil.setBeanLocator(beanLocator);
CollegeLocalService collegeLocalService = mock(CollegeLocalService.class);
DynamicQuery dynamicQuery = mock(DynamicQuery.class);
when(beanLocator.locate(CollegeLocalService.class.getName())).thenReturn(collegeLocalService);
when((List<string>)CollegeLocalService.dynamicQuery(dynamicQuery)).thenReturn(returnCollegeDetails());
assertTrue(CollegeLocalService.dynamicQuery(dynamicQuery).size()>0);</string>
returnCollegeDetails() function
private List<string> returnCollegeDetails()
{
List<string> collegeDetails = new ArrayList<string>();
collegeDetails.add("ABC");
collegeDetails.add("DEC");
collegeDetails.add("XYZ");
return collegeDetails;
}</string></string></string>
But I don't know how to cover the RestrictionsFactoryUtil, ProjectionFactoryUtil and OrderFactoryUtil.
Can anyone please tell me how to do this
Jignesh Thakkar,修改在7 年前。
RE: JUnit test case for Dynamic Query
Junior Member 帖子: 26 加入日期: 10-5-10 最近的帖子
Hi Alex
Did you get solution for this, I am also facing the same problem.
Regards
Jignesh Thakkar
Did you get solution for this, I am also facing the same problem.
Regards
Jignesh Thakkar