From 19eb14d09d240e613d666a23aabf3716696f57a7 Mon Sep 17 00:00:00 2001 From: 3hsan <56245694+ehsansasanian@users.noreply.github.com> Date: Fri, 24 Mar 2023 07:15:13 +0100 Subject: [PATCH] JAVA-18602: update spring-retry version (#13637) * java-18602 update spring-retry version * java-18602 spring-scheduling, improve test coverage * Replace usage of deprecated function in springframework-util with apache-common-lang library --- spring-scheduling/pom.xml | 2 ++ .../com/baeldung/async/config/SpringAsyncConfig.java | 1 - .../java/com/baeldung/springretry/MyService.java | 7 +++---- .../java/com/baeldung/springretry/MyServiceImpl.java | 2 +- .../springretry/SpringRetryIntegrationTest.java | 12 +++++++++++- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/spring-scheduling/pom.xml b/spring-scheduling/pom.xml index 8df35c4844..9010338fee 100644 --- a/spring-scheduling/pom.xml +++ b/spring-scheduling/pom.xml @@ -23,6 +23,7 @@ org.springframework.retry spring-retry + ${spring-retry.version} org.springframework @@ -45,6 +46,7 @@ + 2.0.0 1.3.2 diff --git a/spring-scheduling/src/main/java/com/baeldung/async/config/SpringAsyncConfig.java b/spring-scheduling/src/main/java/com/baeldung/async/config/SpringAsyncConfig.java index 872f59ebb6..79d4dc8058 100644 --- a/spring-scheduling/src/main/java/com/baeldung/async/config/SpringAsyncConfig.java +++ b/spring-scheduling/src/main/java/com/baeldung/async/config/SpringAsyncConfig.java @@ -7,7 +7,6 @@ import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; -import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; diff --git a/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java b/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java index 40e2c419fc..25364442c9 100644 --- a/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java +++ b/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java @@ -2,7 +2,6 @@ package com.baeldung.springretry; import java.sql.SQLException; -import org.springframework.context.annotation.PropertySource; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Recover; import org.springframework.retry.annotation.Retryable; @@ -13,13 +12,13 @@ public interface MyService { @Retryable void retryService(); - @Retryable(value = SQLException.class) + @Retryable(retryFor = SQLException.class) void retryServiceWithRecovery(String sql) throws SQLException; - @Retryable(value = { SQLException.class }, maxAttempts = 2, backoff = @Backoff(delay = 100)) + @Retryable(retryFor = SQLException.class , maxAttempts = 2, backoff = @Backoff(delay = 100)) void retryServiceWithCustomization(String sql) throws SQLException; - @Retryable( value = SQLException.class, maxAttemptsExpression = "${retry.maxAttempts}", + @Retryable(retryFor = SQLException.class, maxAttemptsExpression = "${retry.maxAttempts}", backoff = @Backoff(delayExpression = "${retry.maxDelay}")) void retryServiceWithExternalConfiguration(String sql) throws SQLException; diff --git a/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java b/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java index 7eb4328a47..060187804d 100644 --- a/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java +++ b/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java @@ -5,7 +5,7 @@ import java.sql.SQLException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import org.springframework.util.StringUtils; +import org.apache.commons.lang3.StringUtils; @Service public class MyServiceImpl implements MyService { diff --git a/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java b/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java index 33ce2fff74..c5dff82d9f 100644 --- a/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java +++ b/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java @@ -1,8 +1,14 @@ package com.baeldung.springretry; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.retry.support.RetryTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -14,8 +20,10 @@ import java.sql.SQLException; @ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class) public class SpringRetryIntegrationTest { - @Autowired + @SpyBean private MyService myService; + @Value("${retry.maxAttempts}") + private String maxAttempts; @Autowired private RetryTemplate retryTemplate; @@ -33,11 +41,13 @@ public class SpringRetryIntegrationTest { @Test public void givenRetryServiceWithCustomization_whenCallWithException_thenRetryRecover() throws SQLException { myService.retryServiceWithCustomization(null); + verify(myService, times(Integer.parseInt(maxAttempts))).retryServiceWithCustomization(any()); } @Test public void givenRetryServiceWithExternalConfiguration_whenCallWithException_thenRetryRecover() throws SQLException { myService.retryServiceWithExternalConfiguration(null); + verify(myService, times(Integer.parseInt(maxAttempts))).retryServiceWithExternalConfiguration(any()); } @Test(expected = RuntimeException.class)