Skip to content

Commit

Permalink
Merge pull request #2274 from lukas-krecan/webmvc-test
Browse files Browse the repository at this point in the history
 #2272 Do not fail in startup time if LockProvider is not found
  • Loading branch information
lukas-krecan authored Nov 25, 2024
2 parents 2cb3ac4 + 606c127 commit 151f421
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 151f421

Please sign in to comment.