Added JUnit test cases for ConsulDiscoveryClientConfiguration (#736)
* Added JUnit test cases for ConsulDiscoveryClientConfiguration * Modified potential error message for test failure
This commit is contained in:
@@ -16,11 +16,13 @@
|
||||
|
||||
package org.springframework.cloud.consul.discovery;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
|
||||
import org.springframework.cloud.consul.ConsulAutoConfiguration;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@@ -34,25 +36,85 @@ public class ConsulDiscoveryClientConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
@After
|
||||
@AfterEach
|
||||
public void after() {
|
||||
if (this.context != null && this.context.isActive()) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
/*
|
||||
* @Test public void consulConfigNotLoadedWhenCloudDiscoveryClientDisabled() {
|
||||
* TestPropertyValues.of("spring.cloud.discovery.enabled=false").applyTo(this.context)
|
||||
* ; setupContext(); assertBeanNotPresent(ConsulDiscoveryProperties.class);
|
||||
* assertBeanNotPresent(ConsulDiscoveryClient.class);
|
||||
* assertBeanNotPresent(HeartbeatProperties.class); }
|
||||
*
|
||||
* @Test public void consulConfigNotLoadedWhenConsulDiscoveryClientDisabled() {
|
||||
* TestPropertyValues.of("spring.cloud.consul.discovery.enabled=false").applyTo(this.
|
||||
* context); setupContext(); assertBeanNotPresent(ConsulDiscoveryProperties.class);
|
||||
* assertBeanNotPresent(ConsulDiscoveryClient.class);
|
||||
* assertBeanNotPresent(HeartbeatProperties.class); }
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void consulConfigNotLoadedWhenCloudDiscoveryClientDisabled() {
|
||||
TestPropertyValues.of("spring.cloud.discovery.enabled=false").applyTo(this.context);
|
||||
setupContext();
|
||||
assertBeanNotPresent(ConsulDiscoveryProperties.class);
|
||||
assertBeanNotPresent(ConsulDiscoveryClient.class);
|
||||
assertBeanNotPresent(HeartbeatProperties.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consulConfigIsLoadedWhenCloudDiscoveryClientEnabled() {
|
||||
TestPropertyValues.of("spring.cloud.discovery.enabled=true").applyTo(this.context);
|
||||
setupContext();
|
||||
assertBeanIsPresent(ConsulDiscoveryProperties.class);
|
||||
assertBeanIsPresent(ConsulDiscoveryClient.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consulConfigNotLoadedWhenConsulDiscoveryClientDisabled() {
|
||||
TestPropertyValues.of("spring.cloud.consul.discovery.enabled=false").applyTo(this.context);
|
||||
setupContext();
|
||||
assertBeanNotPresent(ConsulDiscoveryProperties.class);
|
||||
assertBeanNotPresent(ConsulDiscoveryClient.class);
|
||||
assertBeanNotPresent(HeartbeatProperties.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consulConfigIsLoadedWhenConsulDiscoveryClientEnabled() {
|
||||
TestPropertyValues.of("spring.cloud.consul.discovery.enabled=true").applyTo(this.context);
|
||||
setupContext();
|
||||
assertBeanIsPresent(ConsulDiscoveryProperties.class);
|
||||
assertBeanIsPresent(ConsulDiscoveryClient.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consulConfigNotLoadedWhenCloudDiscoveryDisabled_ConsulDiscoveryClientDisabled() {
|
||||
TestPropertyValues.of("spring.cloud.discovery.enabled=false", "spring.cloud.consul.discovery.enabled=false")
|
||||
.applyTo(this.context);
|
||||
setupContext();
|
||||
assertBeanNotPresent(ConsulDiscoveryProperties.class);
|
||||
assertBeanNotPresent(ConsulDiscoveryClient.class);
|
||||
assertBeanNotPresent(HeartbeatProperties.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consulConfigIsLoadedWhenCloudDiscoveryEnabled_ConsulDiscoveryClientEnabled() {
|
||||
TestPropertyValues.of("spring.cloud.discovery.enabled=true", "spring.cloud.consul.discovery.enabled=true")
|
||||
.applyTo(this.context);
|
||||
setupContext();
|
||||
assertBeanIsPresent(ConsulDiscoveryProperties.class);
|
||||
assertBeanIsPresent(ConsulDiscoveryClient.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consulConfigNotLoadedWhenCloudDiscoveryEnabled_ConsulDiscoveryClientDisabled() {
|
||||
TestPropertyValues.of("spring.cloud.discovery.enabled=true", "spring.cloud.consul.discovery.enabled=false")
|
||||
.applyTo(this.context);
|
||||
setupContext();
|
||||
assertBeanNotPresent(ConsulDiscoveryProperties.class);
|
||||
assertBeanNotPresent(ConsulDiscoveryClient.class);
|
||||
assertBeanNotPresent(HeartbeatProperties.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consulConfigNotLoadedWhenCloudDiscoveryDisabled_ConsulDiscoveryClientEnabled() {
|
||||
TestPropertyValues.of("spring.cloud.discovery.enabled=false", "spring.cloud.consul.discovery.enabled=true")
|
||||
.applyTo(this.context);
|
||||
setupContext();
|
||||
assertBeanNotPresent(ConsulDiscoveryProperties.class);
|
||||
assertBeanNotPresent(ConsulDiscoveryClient.class);
|
||||
assertBeanNotPresent(HeartbeatProperties.class);
|
||||
}
|
||||
|
||||
private void setupContext(Class<?>... config) {
|
||||
ConfigurationPropertySources.attach(this.context.getEnvironment());
|
||||
@@ -74,4 +136,13 @@ public class ConsulDiscoveryClientConfigurationTests {
|
||||
}
|
||||
}
|
||||
|
||||
private void assertBeanIsPresent(Class beanClass) {
|
||||
try {
|
||||
context.getBean(beanClass);
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException exception) {
|
||||
fail("Bean of type " + beanClass + " should have been created.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user