Skip to content

Commit

Permalink
Backend:
Browse files Browse the repository at this point in the history
- Add Unit test for "CheckReminders" in ReminderScheduler
- Add Unit test for "getUpcomingReminders" in ReminderService
  • Loading branch information
TingSHI2015 committed Jul 4, 2024
1 parent 6b55942 commit 7d7f1b1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.tingshi2015.backend.reminder;

import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

import static org.mockito.Mockito.*;

class ReminderSchedulerUnitTest {
ReminderService reminderService = mock(ReminderService.class);
NotificationService notificationService = mock(NotificationService.class);
ReminderScheduler reminderScheduler = new ReminderScheduler(reminderService, notificationService);


@Test
void checkReminders() {
//GIVEN
Reminder reminder1 = new Reminder("id1", "Drink Water!", LocalTime.now().withSecond(0).withNano(0), LocalDate.now());
Reminder reminder2 = new Reminder("id2", "Call Mama & Papa!",LocalTime.now().withSecond(0).withNano(0), LocalDate.now());

List<Reminder> reminders = List.of(reminder1, reminder2);

when(reminderService.getUpcomingReminders()).thenReturn(reminders);
//doNothing().when(notificationService).sendNotification(reminder1); //---can be omitted!
//doNothing().when(notificationService).sendNotification(reminder2); //---can be omitted!

//WHEN
reminderScheduler.checkReminders();

//THEN
verify(notificationService, times(1)).sendNotification(reminder1);
verify(notificationService, times(1)).sendNotification(reminder2);


}

}




Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.LocalTime;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -117,4 +118,22 @@ void updateAReminder_withInvalidId_shouldThrowException(){
assertEquals("Reminder with id: " + id + " not found. Can't update!", exception.getMessage());
}


@Test
void getUpcomingReminders(){
//GIVEN
Reminder upcomingReminder1 = new Reminder("id1", "Drink Water!", LocalTime.now().withSecond(0).withNano(0), LocalDate.now());
Reminder upcomingReminder2 = new Reminder("id2", "Call Mama & Papa!",LocalTime.now().withSecond(0).withNano(0), LocalDate.now());
List<Reminder> upcomingReminders = List.of(upcomingReminder1, upcomingReminder2);

when(reminderRepository.findAll()).thenReturn(upcomingReminders);

//WHEN
List<Reminder> actual = reminderService.getUpcomingReminders();

//THEN
verify(reminderRepository).findAll();
assertEquals(upcomingReminders, actual);
}

}

0 comments on commit 7d7f1b1

Please sign in to comment.