我需要通过创建Cucumber集成测试来测试Spring应用程序代码。在触发实际逻辑之前,我使用SpringApplicationBuilder启动我的应用程序,并使用以下语法这样做:
application = new SpringApplicationBuilder()
.parent(new Object[]{"classpath:file1.xml", "classpath:file2.xml"})
.profiles("abc")
.properties("name:value") [It has 5/6 (name:value) pairs here]*
.showBanner(false)
.logStartupInfo(true)
.headless(true)
.application()
.run();我的Spring应用程序正确启动。但是,它没有获得我要传递给SpringApplicationBuilder()的属性(名称、值)对的值。我试过以下方法来设置:
这些选项都不起作用,所以当应用程序启动时,当代码试图访问某些系统属性值时,就会中断。
有什么办法解决这个问题吗..。所有的帮助都非常感谢!我需要这些属性是Spring属性,以确保应用程序正确工作。也许我可以用其他方式使用Spring道具来测试我的代码?如果是的话,我该如何做?
发布于 2015-03-10 05:13:13
您可以如下所示配置这些属性:
application = new SpringApplicationBuilder()
.parent(new Object[]{"classpath:file1.xml", "classpath:file2.xml"})
.profiles("abc")
.properties("key1:test1", "key2:test2")
.showBanner(false)
.logStartupInfo(true)
.headless(true)
.application()
.run();现在,使用@Value注释检索属性:
@Value("${key1}")
String val;val变量将被赋值为test1
发布于 2016-09-21 16:41:16
我自己也处理过同样的问题。我的问题是,我试图设置的值的键也在application.properties文件中设置。
从文件中删除条目之后,它就正常工作了。
https://stackoverflow.com/questions/28936320
复制相似问题