Last active
May 30, 2019 10:24
-
-
Save harkinj/d30ac155e30fb4282769b555f2bf9fb4 to your computer and use it in GitHub Desktop.
mockito returning lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/questions/7366237/mockito-stubbing-methods-that-return-type-with-bounded-wild-cards/7655709 | |
package com.allstate.is.schedapi.services; | |
import com.allstate.is.schedapi.exceptions.ScheduleDeletionException; | |
import com.allstate.is.schedapi.exceptions.ScheduleRetrievalException; | |
import com.allstate.is.schedapi.jobs.WebCallJob; | |
import com.allstate.is.schedapi.models.ScheduleEntry; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.InjectMocks; | |
import org.mockito.Mock; | |
import org.mockito.Mockito; | |
import org.mockito.junit.MockitoJUnitRunner; | |
import org.quartz.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import static org.assertj.core.api.Assertions.assertThat; | |
import static org.mockito.ArgumentMatchers.isA; | |
import static org.mockito.Mockito.when; | |
import static org.quartz.CronScheduleBuilder.cronSchedule; | |
@RunWith(MockitoJUnitRunner.class) | |
public class ScheduleServiceImpl_UT { | |
private final String APP_NAME = "AppName"; | |
private final String SCHEDULE_NAME = "ScheduleName"; | |
private static final String URL_TO_CALL_KEY = "urlToCallKey"; | |
private static final String CRON_NEVER_ACTUALLY_RUN = "0 15 10 * * ? 2035"; | |
private static final String DUMMY_URL_TO_CALL = "http://localhost/test"; | |
@Mock | |
private Scheduler scheduler; | |
@InjectMocks | |
private ScheduleServiceImpl scheduleService; | |
@Test(expected = ScheduleRetrievalException.class) | |
public void getScheduledEntry_noTriggerSetup_thenThrowsScheduleRetrievalException() throws Exception { | |
//Setup | |
JobDetail mockJobDetail = buildJobDetail(); | |
when(scheduler.getJobDetail(isA(JobKey.class) )) | |
.thenReturn(mockJobDetail); | |
// Execute | |
ScheduleEntry scheduleEntry = scheduleService.get(APP_NAME, SCHEDULE_NAME); | |
} | |
@Test | |
public void getScheduledEntrySuccess() throws Exception { | |
//Setup | |
JobDetail mockJobDetail = buildJobDetail(); | |
Trigger mockTrigger = buildJobTrigger(mockJobDetail); | |
List<Trigger> mockTriggers = new ArrayList<>(); | |
mockTriggers.add(mockTrigger); | |
when(scheduler.getJobDetail(isA(JobKey.class) )) | |
.thenReturn(mockJobDetail); | |
Mockito.doReturn(mockTriggers).when(scheduler).getTriggersOfJob(isA(JobKey.class)); | |
//Execute | |
ScheduleEntry scheduleEntry = scheduleService.get(APP_NAME, SCHEDULE_NAME); | |
//Assert | |
assertThat(scheduleEntry.getAppName()).isEqualTo(APP_NAME); | |
assertThat(scheduleEntry.getScheduleName()).isEqualTo(SCHEDULE_NAME); | |
assertThat(scheduleEntry.getCronString()).isEqualTo(CRON_NEVER_ACTUALLY_RUN); | |
assertThat(scheduleEntry.getAppName()).isEqualTo(APP_NAME); | |
assertThat(scheduleEntry.getUrlToCall()).isEqualTo(DUMMY_URL_TO_CALL); | |
} | |
@Test() | |
public void deleteScheduledEntrySuccess() throws Exception { | |
//Setup | |
when(scheduler.deleteJob(isA(JobKey.class) )) | |
.thenReturn(true); | |
// Execute | |
scheduleService.delete(APP_NAME, SCHEDULE_NAME); | |
} | |
@Test(expected = ScheduleDeletionException.class) | |
public void deleteScheduledEntryExceptionThrown() throws Exception { | |
//Setup | |
when(scheduler.deleteJob(isA(JobKey.class) )) | |
.thenReturn(false); | |
// Execute | |
scheduleService.delete(APP_NAME, SCHEDULE_NAME); | |
} | |
private JobDetail buildJobDetail() { | |
JobDetail jobDetail = JobBuilder.newJob(WebCallJob.class) | |
.withIdentity(SCHEDULE_NAME, APP_NAME) | |
.withDescription(SCHEDULE_NAME + "-" + APP_NAME) | |
.usingJobData(URL_TO_CALL_KEY, DUMMY_URL_TO_CALL) | |
.build(); | |
return jobDetail; | |
} | |
private Trigger buildJobTrigger(final JobDetail jobDetail) { | |
Trigger trigger = TriggerBuilder.newTrigger() | |
.forJob(jobDetail) | |
.withIdentity(SCHEDULE_NAME, APP_NAME) | |
.withDescription(jobDetail.getDescription() + "_" + "Trigger") | |
.withSchedule(cronSchedule(CRON_NEVER_ACTUALLY_RUN)) | |
.build(); | |
return trigger; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment