@EventListener
	void updateTask(TaskChangeEvent event) {
	    removeTaskFromScheduler(event.getTask().getId());

        if (!event.isRemoved() && event.getTask().getActive()) {
            addTaskToScheduler(event.getTask().getId(), new SimpleTaskRunnable(event.getTask(), clientFactory.getClientForApplication(event.getTask().getApplication())), event.getTask().getCron());
        }
	}
@Test
    public void updatedTaskIsRemovedAndAddedToScheduleWhenActive() {
        Task taskA = new Task();
        taskA.setId(1l);
        taskA.setName("old");
        taskA.setCron("0 0 * * * *");
        Map<Long, ScheduledFuture<?>> jobsMap = new HashMap<>();

        ScheduledFuture mockA = mock(ScheduledFuture.class);
        jobsMap.put(taskA.getId(), mockA);
        ReflectionTestUtils.setField(service, "jobsMap", jobsMap);
        when(scheduler.schedule(any(Runnable.class), any(CronTrigger.class))).thenReturn(mock(ScheduledFuture.class));

        assertNotNull(((Map<Long, ScheduledFuture<?>>) ReflectionTestUtils.getField(service, "jobsMap")).get(1l));

        Task taskAUpdated = new Task();
        taskAUpdated.setId(1l);
        taskAUpdated.setName("new");
        taskAUpdated.setCron("0 0 * * * *");
        taskAUpdated.setActive(true);
        service.updateTask(new TaskChangeEvent(this, taskAUpdated, false));

        assertEquals(1, ((Map<Long, ScheduledFuture<?>>) ReflectionTestUtils.getField(service, "jobsMap")).size());
        assertNotEquals(((Map<Long, ScheduledFuture<?>>) ReflectionTestUtils.getField(service, "jobsMap")).get(1l), mockA);
        assertNotNull(((Map<Long, ScheduledFuture<?>>) ReflectionTestUtils.getField(service, "jobsMap")).get(1l));
        verify(scheduler, times(1)).schedule(any(Runnable.class), any(CronTrigger.class));
    }