ConfigurationPropertiesRebinderNoDefaultConstructorIntegrationTests.java
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.context.properties;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderNoDefaultConstructorIntegrationTests.TestConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.annotation.DirtiesContext;
import static org.assertj.core.api.BDDAssertions.then;
/**
* Verifies that a {@code @ConfigurationProperties} bean without a default constructor
* (for example a bean with a required dependency) can be rebound without failing or
* logging a warning.
*
* @author Ryan Baxter
*/
@SpringBootTest(classes = TestConfiguration.class, properties = "test.message=Hello")
@ExtendWith(OutputCaptureExtension.class)
public class ConfigurationPropertiesRebinderNoDefaultConstructorIntegrationTests {
@Autowired
private TestProperties properties;
@Autowired
private ConfigurationPropertiesRebinder rebinder;
@Autowired
private ConfigurableEnvironment environment;
@Test
@DirtiesContext
public void rebindsWithoutWarningWhenNoDefaultConstructor(CapturedOutput output) {
then(this.properties.getMessage()).isEqualTo("Hello");
// Change the property and rebind
TestPropertyValues.of("test.message=World").applyTo(this.environment);
this.rebinder.rebind();
// Rebinding still applies the new value
then(this.properties.getMessage()).isEqualTo("World");
// The required dependency is left intact
then(this.properties.getDependency()).isEqualTo("dependency");
// No warning is logged about being unable to create a default instance
then(output).doesNotContain("Cannot create default instance");
}
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@Import({ RefreshConfiguration.RebinderConfiguration.class, PropertyPlaceholderAutoConfiguration.class })
protected static class TestConfiguration {
@Bean
protected TestProperties testProperties() {
return new TestProperties("dependency");
}
}
// Hack out a protected inner class for testing
protected static class RefreshConfiguration extends RefreshAutoConfiguration {
@Configuration(proxyBeanMethods = false)
protected static class RebinderConfiguration extends ConfigurationPropertiesRebinderAutoConfiguration {
public RebinderConfiguration(ApplicationContext context) {
super(context);
}
}
}
@ConfigurationProperties("test")
protected static class TestProperties {
private final String dependency;
private String message;
protected TestProperties(String dependency) {
this.dependency = dependency;
}
public String getDependency() {
return this.dependency;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
}
}