如何在EasyMock中模拟链式方法调用?我有一个链式方法调用:
srvc.getServiceManager().getDetails(Integer.parseInt(msgId));
我已经尝试过了
expect(srvc.getServiceManager().getDetails(Integer.parseInt(msgId)))
.andReturn((String) notNull());
这会抛出NullPointerException.
试图将其分解为两个期望也行不通。在那种情况下,我得到
matcher调用是在预期之外使用的
如何解决此问题?
答案 0 :(得分:1)
打破两个期望是完全正常的,如下所示:
final Service mockService = createMock(Service.class);
final ServiceManager mockServiceManager = createMock(ServiceManager.class);
final Capture<Integer> capturedMsgId = new Capture<Integer>();
expect(mockService.getServiceManager()).andReturn(mockServiceManager);
expect(mockServiceManager.getDetails(capture(capturedMsgId))).andReturn("someString");
你不应该有任何例外。