我正在尝试测试createQuery db调用但是获取空指针异常
@Override
public Member fetchMemberByNetworkId(String networkId) {
List<Member> memberList = new ArrayList<Member>();
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Member> c = cb.createQuery(Member.class);
Root<Member> root = c.from(Member.class);
Expression<String> path = root.get("networkid");
Expression<String> upper =cb.upper(path);
Predicate cp = cb.equal(upper, networkId.toUpperCase());
c.where(cb.and(cp));
Query query = entityManager.createQuery(c);
memberList = query.getResultList();
//List<Member> memberList = query.getResultList();
if(null != memberList && CollectionUtils.isNotEmpty(memberList)) {
return memberList.get(0);
}
else{
return new Member() ;
}
}
public class ExperimentBeanTransformerTest {
//~ Instance fields ------------------------------------------------------------------------------------------------
ExperimentBeanTransformer experimentBeanTransformer = new ExperimentBeanTransformer();
@Spy
@InjectMocks
UserGroupManagementService memberService = new UserGroupManagementServiceImplementation();
@Spy
@InjectMocks
UserGroupManagementDAO userGroupManagementDAO = new UserGroupManagementDAOImplementation();
@Mock
transient EntityManager entityManager ;
@Mock
CriteriaBuilder criteriaBuilder ;
@Mock
CriteriaQuery<Member> criteriaQuery;
@Mock
Root<Member> root;
@Mock
Expression expr;
@Mock
Path path;
@Mock
Predicate predicate;
transient Query query = mock(Query.class);
@Mock
TypedQuery<Member> querymem;
//~ Methods --------------------------------------------------------------------------------------------------------
@BeforeMethod
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testfecthFullNameOfUser() {
String networkId = "network";
List<Member> members = buildMembersList(5);
String queryname="Experiment.findCount";
final List<Member> members2 = new ArrayList<Member>(buildMembersList(5));
Expression<String> doublePath = root.get(networkId);
when(entityManager.getCriteriaBuilder()).thenReturn(criteriaBuilder);
when(criteriaBuilder.createQuery(Member.class)).thenReturn(criteriaQuery);
when(criteriaQuery.from(Member.class)).thenReturn(root);
when(root.get((networkId))).thenReturn(doublePath);
when(criteriaBuilder.upper(root.<String>get("networkid"))).thenReturn(expr);
when(criteriaBuilder.equal(expr, networkId.toUpperCase())).thenReturn(predicate);
when(entityManager.createQuery((String) any())).thenReturn( querymem);
when(query.getResultList()).thenReturn(members2);
when(memberService.fetchMemberByNetworkId(networkId)).thenReturn(members);
when(userGroupManagementDAO.fetchMemberByNetworkId(networkId)).thenReturn((Member) members);
experimentBeanTransformer.fecthFullNameOfUser(networkId);
}
我正在获取查询,路径为空值。我是否必须实例化Path,我尝试但无法为其赋值?如何测试模拟路径(root.get())
?
答案 0 :(得分:0)
在fetchMemberByNetworkId(String networkId)方法中,您使用字符串文字&#34; networkid&#34;进行了硬编码的root.get()方法调用。
表达式路径= root.get(&#34; networkid&#34;);
但你的测试方法是用
模拟的String networkId = "network";
when(root.get((networkId))).thenReturn(doublePath);
在您的实现代码中,在调用root.get方法时,传递实际的String参数networkId而不是string literal&#39; networkid&#39;
public Member fetchMemberByNetworkId(String networkId) {
// Change as below
Expression<String> path = root.get(networkId);
观察: 在测试代码中,您已为networkId分配了一个值,并使用不同的值模拟了对象。
String networkId =&#34; network&#34;;
//请检查以下模拟语句。
when(root.get((networkId))).thenReturn(doublePath);
when(criteriaBuilder.upper(root.<String>get("networkid"))).thenReturn(expr);