如何使用 JMock 在模拟方法内测试模拟方法
我无法确定如何模拟特定的代码段。
这是我的方法:
public void sendNotifications(NotificationType type, Person person)
{
List<Notification> notifications = findNotifications(type);
for(Notification notification : notifications)
{
notification.send(person);
}
}
我希望能够使用 JMock 来测试 findNotifications 是否被调用、and 返回预期值以及 send() 是否被调用。
findNotifications 调用我的 Dao,它被嘲笑了。通知是一个抽象类。
我有一个这样的单元测试,但它显然不起作用。它满足了前两个期望,但仅此而已。
@Test
public void testSendNotifications2()
{
final Person person = new Person();
notificationEntries.add(createEntry1);
Mockery context = new JUnit4Mockery() {{ setImposteriser(ClassImposteriser.INSTANCE); }};
final NotificationDao dao = context.mock(NotificationDao.class, "notificationDao");
final Notification mockedNotification = context.mock(V2ADTNotification.class, "mockNotification");
notifications.add(mockedNotification);
final NotifierServiceImpl mockedService = context.mock(NotifierServiceImpl.class, "mockedService");
//NotifierService service = new NotifierServiceImpl(dao);
context.checking(new Expectations() {
{
one(mockedService).setDao(dao);
one(mockedService).sendNotifications(NotificationType.CREATE, person);
one(mockedService).findNotifications(NotificationType.CREATE);
one(dao).getByNotificationType(NotificationType.CREATE);
will(returnValue(notificationEntries));
will(returnValue(notifications));
one(mockedNotification).send(person);
}
});
mockedService.setDao(dao);
mockedService.sendNotifications(NotificationType.CREATE, person);
context.assertIsSatisfied();
}
mockedService.sendNotifications(NotificationType.CREATE, person);
context.assertIsSatisfied();
}
我怎样才能让它按照我想要的方式工作?
我尝试过的另一种方法。它满足前两个期望,但不满足发送期望。
@Test
public void testSendNotifications()
{
final Person person = new Person();
notificationEntries.add(createEntry1);
Mockery context = new JUnit4Mockery() {{ setImposteriser(ClassImposteriser.INSTANCE); }};
final NotificationDao dao = context.mock(NotificationDao.class, "notificationDao");
final Notification mockedNotification = context.mock(V2ADTNotification.class, "mockNotification");
NotifierService service = new NotifierServiceImpl(dao);
context.checking(new Expectations() {
{
one(dao).getByNotificationType(NotificationType.CREATE);
will(returnValue(notificationEntries));
one(mockedNotification).send(person);
}
});
service.sendNotifications(NotificationType.CREATE, person);
context.assertIsSatisfied();
}
我对使用 Jmock 还很陌生,所以如果我看起来对我正在做的事情没有太多线索(我没有),我深表歉意。
I'm having trouble determining how to mock a particular piece of code.
Here is my method:
public void sendNotifications(NotificationType type, Person person)
{
List<Notification> notifications = findNotifications(type);
for(Notification notification : notifications)
{
notification.send(person);
}
}
I would like to be able to use JMock to test that findNotifications is called and that and returns the expected value and that send() is called.
findNotifications calls my Dao which is mocked. Notification is an abstract class.
I have a unit test like this, but it's obviously not working. It satisfies the first two expectations but no more.
@Test
public void testSendNotifications2()
{
final Person person = new Person();
notificationEntries.add(createEntry1);
Mockery context = new JUnit4Mockery() {{ setImposteriser(ClassImposteriser.INSTANCE); }};
final NotificationDao dao = context.mock(NotificationDao.class, "notificationDao");
final Notification mockedNotification = context.mock(V2ADTNotification.class, "mockNotification");
notifications.add(mockedNotification);
final NotifierServiceImpl mockedService = context.mock(NotifierServiceImpl.class, "mockedService");
//NotifierService service = new NotifierServiceImpl(dao);
context.checking(new Expectations() {
{
one(mockedService).setDao(dao);
one(mockedService).sendNotifications(NotificationType.CREATE, person);
one(mockedService).findNotifications(NotificationType.CREATE);
one(dao).getByNotificationType(NotificationType.CREATE);
will(returnValue(notificationEntries));
will(returnValue(notifications));
one(mockedNotification).send(person);
}
});
mockedService.setDao(dao);
mockedService.sendNotifications(NotificationType.CREATE, person);
context.assertIsSatisfied();
}
mockedService.sendNotifications(NotificationType.CREATE, person);
context.assertIsSatisfied();
}
How could I get this to work as I want it to?
Another way I tried. It satisfies the first two expectations but not the send one.
@Test
public void testSendNotifications()
{
final Person person = new Person();
notificationEntries.add(createEntry1);
Mockery context = new JUnit4Mockery() {{ setImposteriser(ClassImposteriser.INSTANCE); }};
final NotificationDao dao = context.mock(NotificationDao.class, "notificationDao");
final Notification mockedNotification = context.mock(V2ADTNotification.class, "mockNotification");
NotifierService service = new NotifierServiceImpl(dao);
context.checking(new Expectations() {
{
one(dao).getByNotificationType(NotificationType.CREATE);
will(returnValue(notificationEntries));
one(mockedNotification).send(person);
}
});
service.sendNotifications(NotificationType.CREATE, person);
context.assertIsSatisfied();
}
I'm pretty new to using Jmock so I apologize if it doesn't look like I have much of a clue as to what I'm doing (I don't).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你真的不能那样做。真实方法中的调用不会被执行,因为您正在调用模拟而不是真实方法。
您的第一次尝试未能满足最后两个期望,因为使它们通过的调用仅在
sendNotifications
的实际实现中进行,而不是在您对其进行的模拟中进行。第二个更接近可行,但您需要将您的
mockedNotification
添加到您设置为由getByNotificationType< 的模拟返回的
notificationEntries
列表中/代码>。You really just can't do that. The calls in the real method aren't being made, because you're calling the mock instead of the real method.
Your first attempt fails the last two expectations because the calls that would make them pass are made only in the real implementation of
sendNotifications
, and not in the mock that you made of it.The second is closer to workable, but you need to add your
mockedNotification
to thenotificationEntries
list that you've set up to be return by the mock ofgetByNotificationType
.