BAEL-2878 Guide to Spring's ApplicationContextRunner
This commit is contained in:
parent
599cd1cc24
commit
608db42334
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.autoconfiguration.service;
|
||||
|
||||
public class CustomService implements SimpleService {
|
||||
|
||||
@Override
|
||||
public String serve() {
|
||||
return "Custom Service";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.autoconfiguration.service;
|
||||
|
||||
public class DefaultService implements SimpleService {
|
||||
|
||||
@Override
|
||||
public String serve() {
|
||||
return "Default Service";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.autoconfiguration.service;
|
||||
|
||||
public interface SimpleService {
|
||||
|
||||
public String serve();
|
||||
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package com.baeldung.autoconfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
public class ConditionalOnBeanTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenDependentBeanIsPresent_thenConditionalBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(basicConfiguration.class, ConditionalOnBeanConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("created");
|
||||
assertThat(context).getBean("created")
|
||||
.isEqualTo("This is always created");
|
||||
assertThat(context).hasBean("createOnBean");
|
||||
assertThat(context).getBean("createOnBean")
|
||||
.isEqualTo("This is created when bean (name=created) is present");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentBeanIsPresent_thenConditionalMissingBeanIgnored() {
|
||||
this.contextRunner.withUserConfiguration(basicConfiguration.class, ConditionalOnMissingBeanConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("created");
|
||||
assertThat(context).getBean("created")
|
||||
.isEqualTo("This is always created");
|
||||
assertThat(context).doesNotHaveBean("createOnMissingBean");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentBeanIsNotPresent_thenConditionalMissingBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnMissingBeanConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("createOnMissingBean");
|
||||
assertThat(context).getBean("createOnMissingBean")
|
||||
.isEqualTo("This is created when bean (name=created) is missing");
|
||||
assertThat(context).doesNotHaveBean("created");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class basicConfiguration {
|
||||
@Bean
|
||||
public String created() {
|
||||
return "This is always created";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(name = "created")
|
||||
protected static class ConditionalOnBeanConfiguration {
|
||||
@Bean
|
||||
public String createOnBean() {
|
||||
return "This is created when bean (name=created) is present";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(name = "created")
|
||||
protected static class ConditionalOnMissingBeanConfiguration {
|
||||
@Bean
|
||||
public String createOnMissingBean() {
|
||||
return "This is created when bean (name=created) is missing";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package com.baeldung.autoconfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
public class ConditionalOnClassTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsPresent_thenBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnClassConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("created");
|
||||
assertThat(context.getBean("created")).isEqualTo("This is created when ConditionalOnClassTest is present on the classpath");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsPresent_thenBeanMissing() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnMissingClassConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).doesNotHaveBean("missed");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsNotPresent_thenBeanMissing() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnClassConfiguration.class)
|
||||
.withClassLoader(new FilteredClassLoader(ConditionalOnClassTest.class))
|
||||
.run((context) -> {
|
||||
assertThat(context).doesNotHaveBean("created");
|
||||
assertThat(context).doesNotHaveBean(ConditionalOnClassTest.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsNotPresent_thenBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnMissingClassConfiguration.class)
|
||||
.withClassLoader(new FilteredClassLoader(ConditionalOnClassTest.class))
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("missed");
|
||||
assertThat(context).getBean("missed")
|
||||
.isEqualTo("This is missed when ConditionalOnClassTest is present on the classpath");
|
||||
assertThat(context).doesNotHaveBean(ConditionalOnClassTest.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(ConditionalOnClassTest.class)
|
||||
protected static class ConditionalOnClassConfiguration {
|
||||
@Bean
|
||||
public String created() {
|
||||
return "This is created when ConditionalOnClassTest is present on the classpath";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingClass("com.baeldung.autoconfiguration.ConditionalOnClassTest")
|
||||
protected static class ConditionalOnMissingClassConfiguration {
|
||||
@Bean
|
||||
public String missed() {
|
||||
return "This is missed when ConditionalOnClassTest is present on the classpath";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package com.baeldung.autoconfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
|
||||
import com.baeldung.autoconfiguration.service.CustomService;
|
||||
import com.baeldung.autoconfiguration.service.DefaultService;
|
||||
import com.baeldung.autoconfiguration.service.SimpleService;
|
||||
|
||||
public class ConditionalOnPropertyTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenGivenCustomPropertyValue_thenCustomServiceCreated() {
|
||||
this.contextRunner.withPropertyValues("com.baeldung.service=custom")
|
||||
.withUserConfiguration(SimpleServiceConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("customService");
|
||||
SimpleService simpleService = context.getBean(CustomService.class);
|
||||
assertThat(simpleService.serve()).isEqualTo("Custom Service");
|
||||
assertThat(context).doesNotHaveBean("defaultService");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGivenDefaultPropertyValue_thenDefaultServiceCreated() {
|
||||
this.contextRunner.withPropertyValues("com.baeldung.service=default")
|
||||
.withUserConfiguration(SimpleServiceConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("defaultService");
|
||||
SimpleService simpleService = context.getBean(DefaultService.class);
|
||||
assertThat(simpleService.serve()).isEqualTo("Default Service");
|
||||
assertThat(context).doesNotHaveBean("customService");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@TestPropertySource("classpath:ConditionalOnPropertyTest.properties")
|
||||
protected static class SimpleServiceConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "com.baeldung.service", havingValue = "default")
|
||||
@ConditionalOnMissingBean
|
||||
public DefaultService defaultService() {
|
||||
return new DefaultService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "com.baeldung.service", havingValue = "custom")
|
||||
@ConditionalOnMissingBean
|
||||
public CustomService customService() {
|
||||
return new CustomService();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
com.baeldung.service=custom
|
Loading…
Reference in New Issue