获取/保存参数到预期的 JMock 方法调用?
我想测试一个“Adapter”对象,当它收到一条 xml 消息时, 它将其消化为 Message 对象,将消息 ID + CorrelationID 两者都放入 带有时间戳并将其转发给客户端对象。=20 一条消息可以与前一条消息相关联(例如 m2.correlationID =3D m1.ID)。
我模拟客户端,并检查适配器是否成功调用 “client.forwardMessage(m)”两次,第一条消息为 null correlationID,第二个具有非空的correlationID。
但是,我想精确测试相关 ID 是否已设置 正确地,通过获取 ID(例如 m1.ID)。
但无论如何我找不到这样做。
有一个 jira 关于添加该功能,但没有人评论,它 未分配。
难道这真的没有实现吗?
我读到了重新设计适配器以使用 IdGenerator 对象,我可以存根,但我认为会有太多 对象。=20 您不认为将对象拆分为 so 会增加不必要的复杂性吗 细粒度?
谢谢,我很感激任何评论:-)
Tayeb
I want to test an "Adapter" object that when it receives an xml message,
it digest it to a Message object, puts message ID + CorrelationID both
with timestamps and forwards it to a Client object.=20
A message can be correlated to a previous one (e.g. m2.correlationID =3D
m1.ID).
I mock the Client, and check that Adapter successfully calls
"client.forwardMessage(m)" twice with first message with null
correlationID, and a second with a not-null correlationID.
However, I would like to precisely test that the correlationIDs are set
correctly, by grabing the IDs (e.g. m1.ID).
But I couldn't find anyway to do so.
There is a jira about adding the feature, but no one commented and it
is unassigned.
Is this really unimplemented?
I read about the alternative of redesigning the Adapter to use an
IdGenerator object, which I can stub, but I think there will be too many
objects.=20
Don't you think it adds unnecessary complexity to split objects to a so
fine granularity?
Thanks, and I appreciate any comments :-)
Tayeb
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用匹配器 (http://code.google.com/p/hamcrest)检查您是否将正确的参数传递到您的方法中。 请注意,您需要静态导入匹配器(在我的示例中为 HasCorrelationId)。
下面的示例断言 client.forwardMessage(m) 会使用 nullcorrelationId 的消息调用一次,并使用correlationId="abc" 的消息调用一次。
...现在进行测试:
You could use a matcher (http://code.google.com/p/hamcrest) to check whether you get the correct arguments passed into your method. Note you'll need to statically import your matcher (HasCorrelationId in my example).
The example below asserts that client.forwardMessage(m) is called once with a message with null correlationId and once with a message with correlationId="abc".
... and now for the test:
我们做了一些努力来简化 Hamcrest 1.2 中匹配器的编写。 有一个新的FeatureMatcher,需要更少的工作。
We've made some effort to simplify writing matchers in Hamcrest 1.2. There's a new FeatureMatcher which requires less work.