我有一个Spring-Boot应用程序,其中默认属性设置在application.properties类路径中的文件中(src/main/resources/application.properties).
我想在我的JUnit测试中覆盖一些默认设置,其中包含在test.properties文件中声明的属性(src/test/resources/test.properties)
我通常会为我的Junit测试提供专用的Config类,例如
package foo.bar.test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {
}
Run Code Online (Sandbox Code Playgroud)
我首先想到@PropertySource("classpath:test.properties")在TestConfig类中使用可以解决这个问题,但这些属性不会覆盖application.properties设置(请参阅Spring-Boot参考文档 - 23.外部化配置).
然后我尝试-Dspring.config.location=classpath:test.properties在调用测试时使用.这很成功 - 但我不想为每次测试执行设置此系统属性.因此我把它放在代码中
@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {
static {
System.setProperty("spring.config.location", "classpath:test.properties");
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,再次没有成功.
必须有一个关于如何application.properties在JUnit测试中覆盖设置的简单解决方案test.properties,我必须忽略它.
我使用Spring-Cloud-Netflix进行微服务之间的通信.假设我有两个服务,Foo和Bar,而Foo使用Bar的REST端点之一.我使用一个带注释的接口@FeignClient:
@FeignClient
public interface BarClient {
@RequestMapping(value = "/some/url", method = "POST")
void bazzle(@RequestBody BazzleRequest);
}
Run Code Online (Sandbox Code Playgroud)
然后我SomeService在Foo中有一个服务类,它调用了BarClient.
@Component
public class SomeService {
@Autowired
BarClient barClient;
public String doSomething() {
try {
barClient.bazzle(new BazzleRequest(...));
return "so bazzle my eyes dazzle";
} catch(FeignException e) {
return "Not bazzle today!";
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,为了确保服务之间的通信正常工作,我想构建一个测试,使用WireMock之类的东西,对一个假的Bar服务器发出真正的HTTP请求.测试应确保假设正确解码服务响应并报告SomeService.
public class SomeServiceIntegrationTest {
@Autowired SomeService someService;
@Test
public void shouldSucceed() {
stubFor(get(urlEqualTo("/some/url"))
.willReturn(aResponse()
.withStatus(204);
String result = someService.doSomething();
assertThat(result, is("so …Run Code Online (Sandbox Code Playgroud) 我正在使用@TestPropertySource覆盖我的集成测试中的application.yml属性,以用于Spring启动应用程序.
@TestPropertySource(properties = { "repository.file.path=src/test/resources/x" })
Run Code Online (Sandbox Code Playgroud)
我想知道是否有某种方法可以使属性VALUE动态化.像这样的东西:
@TestPropertySource(properties = { "repository.file.path=PropertyValueProvider.class" })
Run Code Online (Sandbox Code Playgroud)
感谢您的反馈.在我的情况下,属性值是系统特定的,应该在测试运行时生成.
考虑以下示例:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {
"some.property=valueA"
})
public class ServiceTest {
@Test
public void testA() { ... }
@Test
public void testB() { ... }
@Test
public void testC() { ... }
}
Run Code Online (Sandbox Code Playgroud)
我正在使用SpringBootTest注释的properties属性来设置some.property此测试套件中所有测试的属性值。现在,我想为其中一个测试(假设testC)设置此属性的另一个值,而不影响其他测试。我该如何实现?我已经阅读了Spring Boot docs的“测试”一章,但是没有找到与我的用例匹配的内容。
java ×3
spring-boot ×3
spring ×2
feign ×1
spring-mvc ×1
spring-test ×1
unit-testing ×1
wiremock ×1