mercredi 3 mars 2021

mockito to test void method that notify users of some event doesnt work

so i have this method that basicaly notifies atendees of an event of some info:

public class EventNotificationServiceImpl implements EventNotificationService {


    private static final String MSG_ANNOUNCE = "The next big event is coming!";
    private static final String MSG_CONFIRM = "Dear Attendee, your subscription to the event has been confirmed successfully.";

    @Override
    public void announce(Event event) {
        if (event == null || event.getAttendees() == null || event.getAttendees().isEmpty())
            return;

        for (Attendee attendee : event.getAttendees()) {
            Notification announce = new Notification(MSG_ANNOUNCE);
            attendee.getNotifications().add(announce);
        }

    }

Class Atendee has this method to return all notifications:

 public List<Notification> getNotifications() {
        return notifications;
    }

So ive tried to test with mockito that method announce correctly informs users of the event, first invoking announce method and then checking if attendees.getNotifications has the message that announce method adds.

@ExtendWith(MockitoExtension.class)
public class EventNotificationTest {

    @Mock
    Attendee atendee;

    @InjectMocks
    EventNotificationServiceImpl eventNotificationService;

    @Test
    public void checkIfAtendesAreNotified(){
        Event event = new Event();
        eventNotificationService.announce(event);


       assertEquals( atendee.getNotifications(), "The next big event is coming!");


    }

Im super new with mockito so im sure im missing something, cause it returns that attendees.getNotifications is empty. I dont understand why since i called announce method and this method adds a notification to atendees list. If someone can help me ill be very grateful.

Aucun commentaire:

Enregistrer un commentaire