我正在使用一个运行我的src/main/resources/config/application.yml的spring启动应用程序.
当我运行我的测试用例时:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public class MyIntTest{
}
Run Code Online (Sandbox Code Playgroud)
测试代码仍然运行我的application.yml文件来加载属性.我想知道在运行测试用例时是否可以运行另一个*.yml文件.
我有一个春季启动项目,它很棒.我现在想为我的应用程序编写测试,我遇到了一些配置问题.
Spring boot为我创建了一个名为ApplicationTests的测试类.这很简单,看起来像这样:
@RunWith(SpringRunner.class)
@SpringBootTest
public class DuurzaamApplicationTests {
@Test
public void contextLoads() {
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我开始测试时,我收到此错误:
java.lang.IllegalArgumentException: Could not resolve placeholder 'company.upload' in value "${company.upload}"
Run Code Online (Sandbox Code Playgroud)
我在src/test/resources目录中有一个properties.yml文件,由于某种原因它没有加载.我从互联网上的例子中尝试了所有不同类型的注释,但它们都没有工作.
如何告诉spring boot测试使用application.yml文件加载属性?
有没有办法在不使用 spring boot 的情况下在普通 spring web mvc 应用程序中启用 yaml 配置(而不是属性文件)并支持 @Value 注释。
我搜索了很多,到处都使用弹簧靴。我对使用 spring boot 不感兴趣,因为它非常重,并且为自动设置配置了许多依赖项。
任何帮助,将不胜感激......
我有这个扫描 Spring 上下文的代码:
public void scan() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SomeConfig.class);
context.refresh();
}
Run Code Online (Sandbox Code Playgroud)
我需要从application.yml文件中读取属性,所以在SomeConfig课堂上,我有这个:
@Configuration
@PropertySource(value = "classpath:application.yml", factory = YamlPropertyLoaderFactory.class)
public class SomeConfig {
//some beans
}
Run Code Online (Sandbox Code Playgroud)
(我从这里复制了 YamlPropertyLoaderFactory 类)
application.yml 是一个典型的 Spring Boot 文件,具有一些按配置文件的属性和一个默认配置文件:
spring:
profiles:
active: p1
---
spring:
profiles: p1
file: file1.txt
---
spring:
profiles: p2
file: file2.txt
Run Code Online (Sandbox Code Playgroud)
在某些 bean 中,我正在file使用@Value.
当我运行我的应用程序时,我正在传递-Dspring.profiles.active=p1变量,但出现错误:
无法解析值“${file}”中的占位符“文件”
(即使我没有传递任何配置文件,它也应该可以工作,因为 application.yml 的默认配置文件设置为 p1)
如果我从 中删除所有配置文件配置application.yml,它工作正常:
file: …Run Code Online (Sandbox Code Playgroud) 是否可以从.json文件而不是.yaml或.properties加载spring-boot配置?从文档上看,这不是开箱即用的-我想知道是否有可能,如果可以,怎么办?
我正在尝试从YML加载配置。如果这些是逗号分隔的值,则可以加载值,也可以加载列表。但是我无法加载典型的YML列表。
配置类别
@Component
@PropertySource("classpath:routing.yml")
@ConfigurationProperties
class RoutingProperties(){
var angular = listOf("nothing")
var value: String = ""
}
Run Code Online (Sandbox Code Playgroud)
工作routing.yml
angular: /init, /home
value: Hello World
Run Code Online (Sandbox Code Playgroud)
无法正常工作routing.yml
angular:
- init
- home
value: Hello World
Run Code Online (Sandbox Code Playgroud)
为什么我不能加载yml的第二个版本/我有语法错误?
ENV:Kotlin,春季2.0.0.M3
我正在使用带有默认application.yml属性文件的Spring Boot 2.0 。我想将它拆分为单独的属性文件,因为它变得很大。
此外,我想编写测试来检查属性的正确性:将出现在生产应用程序上下文(而不是测试上下文)中的值。
这是我的属性文件:src/main/resources/config/custom.yml
my-property:
value: 'test'
Run Code Online (Sandbox Code Playgroud)
属性类:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Data
@Configuration
@ConfigurationProperties(prefix = "my-property")
@PropertySource("classpath:config/custom.yml")
public class MyProperty {
private String value;
}
Run Code Online (Sandbox Code Playgroud)
测试:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyProperty.class)
@EnableConfigurationProperties
public class MyPropertyTest {
@Autowired
private MyProperty property;
@Test
public void test() {
assertEquals("test", property.getValue());
}
}
Run Code Online (Sandbox Code Playgroud)
但测试失败并出现错误:
java.lang.AssertionError:
Expected :test
Actual :null
Run Code Online (Sandbox Code Playgroud)
我还看到属性值是 …
假设实体中有一些命名查询,那些命名查询应该如何评论?有没有办法将它们映射到创建的javadoc?
@Entity
@NamedQueries({
@NamedQuery(name="Country.findAll",
query="SELECT c FROM Country c"),
@NamedQuery(name="Country.findByName",
query="SELECT c FROM Country c WHERE c.name = :name"),
})
public class Country {
...
}
Run Code Online (Sandbox Code Playgroud)
目前我把评论(非javadoc)放在行前但我不太喜欢它.
// find all countries
@NamedQuery(name="Country.findAll", query="SELECT c FROM Country c")
Run Code Online (Sandbox Code Playgroud) 我有一个使用jHipster创建的Spring Boot应用程序,我正在尝试运行一些集成测试用例,并且我使用以下注释配置了测试用例类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@Transactional
public class ScheduleRepositoryTests {....
Run Code Online (Sandbox Code Playgroud)
但我仍然得到上述异常.Stack Trace如下:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name …Run Code Online (Sandbox Code Playgroud) spring ×7
java ×6
spring-boot ×5
spring-mvc ×2
jhipster ×1
jpa ×1
json ×1
kotlin ×1
named-query ×1
properties ×1
unit-testing ×1
yaml ×1