BAEL-7544: Disable enable scheduling on spring tests (#16125)
* BEAL-7544: Add examples showing how to disable scheduled tasks for testing * BEAL-7544: Get rid of final keywords * BEAL-7544: Add the config and test showing the case when the scheduling is simply turned on * BEAL-7544: Introduce amendments after review - add initialDelayString param to the scheduled annotation on the tested method - delete unnecessary unit test * BEAL-7544: Move code to new module
This commit is contained in:
parent
4394e828c1
commit
af3a000b1e
2
pom.xml
2
pom.xml
|
@ -826,6 +826,7 @@
|
|||
<module>spring-reactive-modules</module>
|
||||
<module>spring-remoting-modules</module> <!-- Upgrade to Boot 3 not possible as since Spring 6 the remoting modules has been removed from Spring. -->
|
||||
<module>spring-scheduling</module>
|
||||
<module>spring-scheduling-2</module>
|
||||
<module>spring-security-modules</module>
|
||||
<module>spring-shell</module>
|
||||
<module>spring-soap</module>
|
||||
|
@ -1074,6 +1075,7 @@
|
|||
<module>spring-reactive-modules</module>
|
||||
<module>spring-remoting-modules</module> <!-- Upgrade to Boot 3 not possible as since Spring 6 the remoting modules has been removed from Spring. -->
|
||||
<module>spring-scheduling</module>
|
||||
<module>spring-scheduling-2</module>
|
||||
<module>spring-security-modules</module>
|
||||
<module>spring-shell</module>
|
||||
<module>spring-soap</module>
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
### Relevant articles:
|
|
@ -0,0 +1,40 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-scheduling-2</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-scheduling-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>repackage</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.disablingscheduledtasks;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
public class DelayedNotificationScheduler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DelayedNotificationScheduler.class);
|
||||
|
||||
private NotificationService notificationService;
|
||||
|
||||
public DelayedNotificationScheduler(NotificationService notificationService) {
|
||||
this.notificationService = notificationService;
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "${notification.send.out.delay}", initialDelayString = "${notification.send.out.initial.delay}")
|
||||
public void attemptSendingOutDelayedNotifications() {
|
||||
logger.info("Scheduled notifications send out attempt");
|
||||
notificationService.sendOutDelayedNotifications();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.disablingscheduledtasks;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Notification {
|
||||
|
||||
private UUID id = UUID.randomUUID();
|
||||
private boolean isSentOut = false;
|
||||
private ZonedDateTime sendOutTime;
|
||||
|
||||
public Notification(ZonedDateTime sendOutTime) {
|
||||
this.sendOutTime = sendOutTime;
|
||||
}
|
||||
|
||||
public void sendOut(Clock clock) {
|
||||
ZonedDateTime now = ZonedDateTime.now(clock);
|
||||
if (now.isAfter(sendOutTime)) {
|
||||
isSentOut = true;
|
||||
}
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean isSentOut() {
|
||||
return isSentOut;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.disablingscheduledtasks;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class NotificationRepository {
|
||||
|
||||
private Collection<Notification> notifications = new ConcurrentLinkedQueue<>();
|
||||
|
||||
public Notification findById(UUID notificationId) {
|
||||
return notifications.stream()
|
||||
.filter(n -> notificationId.equals(n.getId()))
|
||||
.findFirst()
|
||||
.orElseThrow(NoSuchElementException::new);
|
||||
}
|
||||
|
||||
public List<Notification> findAllAwaitingSendOut() {
|
||||
return notifications.stream()
|
||||
.filter(notification -> !notification.isSentOut())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void save(Notification notification) {
|
||||
notifications.add(notification);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.disablingscheduledtasks;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class NotificationService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(NotificationService.class);
|
||||
|
||||
private NotificationRepository notificationRepository;
|
||||
private Clock clock;
|
||||
|
||||
public NotificationService(NotificationRepository notificationRepository, Clock clock) {
|
||||
this.notificationRepository = notificationRepository;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
public void sendOutDelayedNotifications() {
|
||||
logger.info("Sending out delayed notifications");
|
||||
List<Notification> notifications = notificationRepository.findAllAwaitingSendOut();
|
||||
notifications.forEach(notification -> notification.sendOut(clock));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.disablingscheduledtasks.config.disablewithprofile;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.disablingscheduledtasks.DelayedNotificationScheduler;
|
||||
import com.baeldung.disablingscheduledtasks.NotificationRepository;
|
||||
import com.baeldung.disablingscheduledtasks.NotificationService;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationConfig {
|
||||
|
||||
@Bean
|
||||
public Clock clock() {
|
||||
return Clock.systemUTC();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NotificationRepository notificationRepository() {
|
||||
return new NotificationRepository();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NotificationService notificationService(NotificationRepository notificationRepository, Clock clock) {
|
||||
return new NotificationService(notificationRepository, clock);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DelayedNotificationScheduler delayedNotificationScheduler(NotificationService notificationService) {
|
||||
return new DelayedNotificationScheduler(notificationService);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.disablingscheduledtasks.config.disablewithprofile;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@Profile("!integrationTest")
|
||||
public class SchedulingConfig {
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.disablingscheduledtasks.config.disablewithproperty;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.disablingscheduledtasks.DelayedNotificationScheduler;
|
||||
import com.baeldung.disablingscheduledtasks.NotificationRepository;
|
||||
import com.baeldung.disablingscheduledtasks.NotificationService;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationConfig {
|
||||
|
||||
@Bean
|
||||
public Clock clock() {
|
||||
return Clock.systemUTC();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NotificationRepository notificationRepository() {
|
||||
return new NotificationRepository();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NotificationService notificationService(NotificationRepository notificationRepository, Clock clock) {
|
||||
return new NotificationService(notificationRepository, clock);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DelayedNotificationScheduler delayedNotificationScheduler(NotificationService notificationService) {
|
||||
return new DelayedNotificationScheduler(notificationService);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.disablingscheduledtasks.config.disablewithproperty;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@ConditionalOnProperty(value = "scheduling.enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class SchedulingConfig {
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.disablingscheduledtasks.config.schedulingon;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
import com.baeldung.disablingscheduledtasks.DelayedNotificationScheduler;
|
||||
import com.baeldung.disablingscheduledtasks.NotificationRepository;
|
||||
import com.baeldung.disablingscheduledtasks.NotificationService;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
public class ApplicationConfig {
|
||||
|
||||
@Bean
|
||||
public Clock clock() {
|
||||
return Clock.systemUTC();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NotificationRepository notificationRepository() {
|
||||
return new NotificationRepository();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NotificationService notificationService(NotificationRepository notificationRepository, Clock clock) {
|
||||
return new NotificationService(notificationRepository, clock);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DelayedNotificationScheduler delayedNotificationScheduler(NotificationService notificationService) {
|
||||
return new DelayedNotificationScheduler(notificationService);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package com.baeldung.disablingscheduledtasks.disablewithprofile;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import com.baeldung.disablingscheduledtasks.DelayedNotificationScheduler;
|
||||
import com.baeldung.disablingscheduledtasks.Notification;
|
||||
import com.baeldung.disablingscheduledtasks.NotificationRepository;
|
||||
import com.baeldung.disablingscheduledtasks.config.disablewithprofile.ApplicationConfig;
|
||||
import com.baeldung.disablingscheduledtasks.config.disablewithprofile.SchedulingConfig;
|
||||
|
||||
@SpringBootTest(
|
||||
classes = { ApplicationConfig.class, SchedulingConfig.class, SchedulerTestConfiguration.class },
|
||||
properties = {
|
||||
"notification.send.out.delay: 10",
|
||||
"notification.send.out.initial.delay: 0"
|
||||
}
|
||||
)
|
||||
@ActiveProfiles("integrationTest")
|
||||
public class DelayedNotificationSchedulerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private Clock testClock;
|
||||
|
||||
@Autowired
|
||||
private NotificationRepository repository;
|
||||
|
||||
@Autowired
|
||||
private DelayedNotificationScheduler scheduler;
|
||||
|
||||
@Test
|
||||
public void whenTimeIsOverNotificationSendOutTime_thenItShouldBeSent() {
|
||||
ZonedDateTime fiveMinutesAgo = ZonedDateTime.now(testClock).minusMinutes(5);
|
||||
Notification notification = new Notification(fiveMinutesAgo);
|
||||
repository.save(notification);
|
||||
|
||||
scheduler.attemptSendingOutDelayedNotifications();
|
||||
|
||||
Notification processedNotification = repository.findById(notification.getId());
|
||||
assertTrue(processedNotification.isSentOut());
|
||||
}
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
class SchedulerTestConfiguration {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public Clock testClock() {
|
||||
return Clock.fixed(Instant.parse("2024-03-10T10:15:30.00Z"), ZoneId.systemDefault());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.disablingscheduledtasks.disablewithproperty;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import com.baeldung.disablingscheduledtasks.DelayedNotificationScheduler;
|
||||
import com.baeldung.disablingscheduledtasks.Notification;
|
||||
import com.baeldung.disablingscheduledtasks.NotificationRepository;
|
||||
import com.baeldung.disablingscheduledtasks.config.disablewithproperty.ApplicationConfig;
|
||||
import com.baeldung.disablingscheduledtasks.config.disablewithproperty.SchedulingConfig;
|
||||
|
||||
@SpringBootTest(
|
||||
classes = { ApplicationConfig.class, SchedulingConfig.class, SchedulerTestConfiguration.class },
|
||||
properties = {
|
||||
"notification.send.out.delay: 10",
|
||||
"notification.send.out.initial.delay: 0",
|
||||
"scheduling.enabled: false"
|
||||
}
|
||||
)
|
||||
public class DelayedNotificationSchedulerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private Clock testClock;
|
||||
|
||||
@Autowired
|
||||
private NotificationRepository repository;
|
||||
|
||||
@Autowired
|
||||
private DelayedNotificationScheduler scheduler;
|
||||
|
||||
@Test
|
||||
public void whenTimeIsOverNotificationSendOutTime_thenItShouldBeSent() {
|
||||
ZonedDateTime fiveMinutesAgo = ZonedDateTime.now(testClock).minusMinutes(5);
|
||||
Notification notification = new Notification(fiveMinutesAgo);
|
||||
repository.save(notification);
|
||||
|
||||
scheduler.attemptSendingOutDelayedNotifications();
|
||||
|
||||
Notification processedNotification = repository.findById(notification.getId());
|
||||
assertTrue(processedNotification.isSentOut());
|
||||
}
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
class SchedulerTestConfiguration {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public Clock testClock() {
|
||||
return Clock.fixed(Instant.parse("2024-03-10T10:15:30.00Z"), ZoneId.systemDefault());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.disablingscheduledtasks.longinitialdelay;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import com.baeldung.disablingscheduledtasks.DelayedNotificationScheduler;
|
||||
import com.baeldung.disablingscheduledtasks.Notification;
|
||||
import com.baeldung.disablingscheduledtasks.NotificationRepository;
|
||||
import com.baeldung.disablingscheduledtasks.config.schedulingon.ApplicationConfig;
|
||||
|
||||
@SpringBootTest(
|
||||
classes = { ApplicationConfig.class, SchedulerTestConfiguration.class },
|
||||
properties = {
|
||||
"notification.send.out.delay: 10",
|
||||
"notification.send.out.initial.delay: 60000"
|
||||
}
|
||||
)
|
||||
public class DelayedNotificationSchedulerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private Clock testClock;
|
||||
|
||||
@Autowired
|
||||
private NotificationRepository repository;
|
||||
|
||||
@Autowired
|
||||
private DelayedNotificationScheduler scheduler;
|
||||
|
||||
@Test
|
||||
public void whenTimeIsOverNotificationSendOutTime_thenItShouldBeSent() {
|
||||
ZonedDateTime fiveMinutesAgo = ZonedDateTime.now(testClock).minusMinutes(5);
|
||||
Notification notification = new Notification(fiveMinutesAgo);
|
||||
repository.save(notification);
|
||||
|
||||
scheduler.attemptSendingOutDelayedNotifications();
|
||||
|
||||
Notification processedNotification = repository.findById(notification.getId());
|
||||
assertTrue(processedNotification.isSentOut());
|
||||
}
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
class SchedulerTestConfiguration {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public Clock testClock() {
|
||||
return Clock.fixed(Instant.parse("2024-03-10T10:15:30.00Z"), ZoneId.systemDefault());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.disablingscheduledtasks.schedulingon;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import com.baeldung.disablingscheduledtasks.DelayedNotificationScheduler;
|
||||
import com.baeldung.disablingscheduledtasks.Notification;
|
||||
import com.baeldung.disablingscheduledtasks.NotificationRepository;
|
||||
import com.baeldung.disablingscheduledtasks.config.schedulingon.ApplicationConfig;
|
||||
|
||||
@SpringBootTest(
|
||||
classes = { ApplicationConfig.class, SchedulerTestConfiguration.class },
|
||||
properties = {
|
||||
"notification.send.out.delay: 10",
|
||||
"notification.send.out.initial.delay: 0"
|
||||
}
|
||||
)
|
||||
public class DelayedNotificationSchedulerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private Clock testClock;
|
||||
|
||||
@Autowired
|
||||
private NotificationRepository repository;
|
||||
|
||||
@Autowired
|
||||
private DelayedNotificationScheduler scheduler;
|
||||
|
||||
@Test
|
||||
public void whenTimeIsOverNotificationSendOutTime_thenItShouldBeSent() {
|
||||
ZonedDateTime fiveMinutesAgo = ZonedDateTime.now(testClock).minusMinutes(5);
|
||||
Notification notification = new Notification(fiveMinutesAgo);
|
||||
repository.save(notification);
|
||||
|
||||
scheduler.attemptSendingOutDelayedNotifications();
|
||||
|
||||
Notification processedNotification = repository.findById(notification.getId());
|
||||
assertTrue(processedNotification.isSentOut());
|
||||
}
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
class SchedulerTestConfiguration {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public Clock testClock() {
|
||||
return Clock.fixed(Instant.parse("2024-03-10T10:15:30.00Z"), ZoneId.systemDefault());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue