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 332124e7b..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 @@ -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 @@ -30,9 +26,4 @@ public class Application { 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"); + } +} 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()); + } +}