From d6e09031673e3d0b5afe19b99a4ced6447620408 Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Mon, 25 Nov 2024 20:08:36 +0100 Subject: [PATCH 1/3] MockMvc test --- .../test/boot/HelloControllerTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 spring/test/shedlock-springboot-test/src/test/java/net/javacrumbs/shedlock/test/boot/HelloControllerTest.java diff --git a/spring/test/shedlock-springboot-test/src/test/java/net/javacrumbs/shedlock/test/boot/HelloControllerTest.java b/spring/test/shedlock-springboot-test/src/test/java/net/javacrumbs/shedlock/test/boot/HelloControllerTest.java new file mode 100644 index 000000000..62473e7c6 --- /dev/null +++ b/spring/test/shedlock-springboot-test/src/test/java/net/javacrumbs/shedlock/test/boot/HelloControllerTest.java @@ -0,0 +1,21 @@ +package net.javacrumbs.shedlock.test.boot; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.test.web.servlet.MockMvc; + +@WebMvcTest(HelloController.class) +class HelloControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Test + void shouldCallController() throws Exception { + mockMvc.perform(get("/")).andExpect(status().isOk()); + } +} From 8be6ffd2fed36025c94ad021c5e0c92702286fa2 Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Mon, 25 Nov 2024 20:22:23 +0100 Subject: [PATCH 2/3] Extract ShedlockConfig in test --- .../shedlock/test/boot/Application.java | 8 -------- .../shedlock/test/boot/ShedlockConfig.java | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/ShedlockConfig.java diff --git a/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/Application.java b/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/Application.java index 332124e7b..5bc5efa83 100644 --- a/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/Application.java +++ b/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/Application.java @@ -13,13 +13,9 @@ */ package net.javacrumbs.shedlock.test.boot; -import javax.sql.DataSource; -import net.javacrumbs.shedlock.core.LockProvider; -import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider; import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @@ -31,8 +27,4 @@ public static void main(String[] args) { SpringApplication.run(Application.class); } - @Bean - public LockProvider lockProvider(DataSource dataSource) { - return new JdbcTemplateLockProvider(dataSource, "shedlock"); - } } diff --git a/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/ShedlockConfig.java b/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/ShedlockConfig.java new file mode 100644 index 000000000..bd0ca31b4 --- /dev/null +++ b/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/ShedlockConfig.java @@ -0,0 +1,15 @@ +package net.javacrumbs.shedlock.test.boot; + +import javax.sql.DataSource; +import net.javacrumbs.shedlock.core.LockProvider; +import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ShedlockConfig { + @Bean + public LockProvider lockProvider(DataSource dataSource) { + return new JdbcTemplateLockProvider(dataSource, "shedlock"); + } +} From 606c127cc0745fc5f3173020c8a7e74e64ef54f6 Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Mon, 25 Nov 2024 20:36:14 +0100 Subject: [PATCH 3/3] #2272 Do not fail in startup time if LockProvider is not found --- .../shedlock/spring/aop/LockProviderSupplier.java | 9 ++++----- .../net/javacrumbs/shedlock/test/boot/Application.java | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/spring/shedlock-spring/src/main/java/net/javacrumbs/shedlock/spring/aop/LockProviderSupplier.java b/spring/shedlock-spring/src/main/java/net/javacrumbs/shedlock/spring/aop/LockProviderSupplier.java index 617759b83..d60be1647 100644 --- a/spring/shedlock-spring/src/main/java/net/javacrumbs/shedlock/spring/aop/LockProviderSupplier.java +++ b/spring/shedlock-spring/src/main/java/net/javacrumbs/shedlock/spring/aop/LockProviderSupplier.java @@ -3,7 +3,6 @@ import java.lang.reflect.Method; import net.javacrumbs.shedlock.core.LockProvider; import org.springframework.beans.factory.ListableBeanFactory; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; /** * Not public now. If you think you need your LockProviderSupplier please create an issue and explain your use-case. @@ -15,10 +14,10 @@ interface LockProviderSupplier { static LockProviderSupplier create(ListableBeanFactory beanFactory) { // Only fetching beanNames as the beans might not have been initialized yet. String[] beanNamesForType = beanFactory.getBeanNamesForType(LockProvider.class); - if (beanNamesForType.length == 0) { - throw new NoSuchBeanDefinitionException(LockProvider.class, "No LockProvider bean found."); - } - if (beanNamesForType.length == 1) { + // If there are no beans of LockProvider type, we can't fail here as in older version we + // did not fail, and it's quire common in tests. To maintain backward compatibility + // the failure will happen in runtime. + if (beanNamesForType.length <= 1) { return (target, method, arguments) -> beanFactory.getBean(LockProvider.class); } return new BeanNameSelectingLockProviderSupplier(beanFactory); diff --git a/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/Application.java b/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/Application.java index 5bc5efa83..26af57d17 100644 --- a/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/Application.java +++ b/spring/test/shedlock-springboot-test/src/main/java/net/javacrumbs/shedlock/test/boot/Application.java @@ -26,5 +26,4 @@ public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } - }