【发布时间】:2021-03-05 10:56:40
【问题描述】:
所以我有这段代码,我试图在其中模拟参加者收到通知:
@Mock
Event event;
@Mock
Attendee attende;
@InjectMocks
EventNotificationServiceImpl eventNotificationService;
@Test
public void checkIfAttendesAreNotified() {
event.addAttendee(attende);
eventNotificationService.announce(event);
System.out.println(attende.getNotifications());
List<Notification> notifications = attende.getNotifications();
for (Notification notification : notifications) {
assertEquals("The next big event is coming!", notification.getMessage());
}
}
但是在这里我没有看到参加者收到任何通知。但是当我输入此代码时,我看到参加者收到通知:
@Mock
Event event;
@Mock
Attendee attende;
@InjectMocks
EventNotificationServiceImpl eventNotificationService;
@Test
public void checkIfAttendesAreNotified() {
attende = new Attendee(1L,"sara", "sara@example.com");
event = new Event();
event.addAttendee(attende);
eventNotificationService.announce(event);
System.out.println(attende.getNotifications());
List<Notification> notifications = attende.getNotifications();
for (Notification notification : notifications) {
assertEquals("The next big event is coming!", notification.getMessage());
}
}
我想知道为什么没有初始化模拟。我不想在测试中创建事件和参与者对象,因为这给我带来了问题,因为当我尝试验证时,参与者和事件不被识别为模拟。如果有人可以帮助我,我将非常感激! :)))
【问题讨论】:
标签: java testing junit mocking mockito