将spring从2.0.8升级到2.1.2(使用JDK 8)时,应用程序启动并运行正常,但由于__urlencode()
而导致测试失败。
我正在使用某些测试可以扩展的抽象类。
java.lang.IllegalStateException: Failed to load ApplicationContext
EnvironmentalProperties类是用于类型安全配置属性(Doc)的类
在可以进行升级之前,提供了一类EnvironmentalProperty,但是现在我得到了
@SpringBootTest
@RunWith(SpringRunner.class)
public abstract class AbstractTestkonfiguration {
@TestConfiguration
static class TestEnvironmentConfiguration {
@Component
@PropertySource(value = "classpath:my-test.properties")
@ConfigurationProperties(prefix = "my")
public static class MyTestProperties extends EnvironmentalProperties {
}
}
}
这是否与嵌套配置类检测(Upgrading to Spring Framework 5.x)中的更改有关?如果是这样,如何配置仅用于测试的EnvironmentalProperties Bean?
更新:即使按以下方式使用也不起作用(结果相同)。
[...]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.abc.EnvironmentalProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1651)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1210)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
... 90 more
答案 0 :(得分:1)
您需要进行一些更改。
@EnableConfigurationProperties
启用配置属性@Component
批注这是一个可行的示例;
src/test/resources/my-test.properties
my.server.name=foo
my.server=test
和
src/main/resources/application.properties
my.name=production
生产配置。
@ConfigurationProperties(prefix = "my")
@PropertySource(value = "classpath:application.properties")
public class EnvironmentalProperties {
private String name;
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
@SpringBootTest
@TestPropertySource(value = {"classpath:my-test.properties", "classpath:application.properties"})
@RunWith(SpringRunner.class)
public class AbstractTestkonfiguration {
@Autowired
private MyTestProperties myTestProperties;
@TestConfiguration
@EnableConfigurationProperties(MyTestProperties.class)
public static class TestEnvironmentConfiguration {
@ConfigurationProperties(prefix = "my")
public static class MyTestProperties extends EnvironmentalProperties {
private String server;
public String getServer() {
return server;
}
public void setServer(final String server) {
this.server = server;
}
}
}
@Test
public void check_configuration () {
Assert.assertEquals(myTestProperties.getServer(), "test");
Assert.assertEquals(myTestProperties.getName(), "production");
}
这适用于Java 11和spring-boot 2.1.2.RELEASE。请注意,这仅是示例。您将不得不正确地使其适应您的项目。