我有这个错误时,运行“mvn测试”在我的Spring Boot 应用程序。
如果我使用spring run配置正常运行应用程序,则不会出现错误,并且能够解析路径。
基本上,path.to.my.properties指向intellijIdea模块中的一个文件,其中包含一些需要处理的原始数据
我能做些什么来避免这个错误呢?也许在测试中以某种方式模拟这个值?
资源config-local.properties:
path.to.my.properties=C:/Dev/ExternalFolder/route.properties
src\main\java\com\myapp\configuration\PropertiesConfiguration.java
@Configuration
@PropertySource("file:${path.to.my.properties}")
public class PropertiesConfiguration {
@Autowired
private Environment env;
public String getValueByKey(String keyValue) {
return env.getProperty(keyValue);
}
}
\src\test\java\com\test\myapp\main\TestSearchMessagesAPI.java
@SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT,
classes = {Application.class})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class TestSearchMessagesAPI {
private static final Logger LOGGER = LoggerFactory.getLogger(
TestSearchMessagesAPI.class);
@Value("${HTTPS_PORT}")
private int port;
@Value("${server.servlet.context-path}")
private String servletPath;
@Value("${junit.service-path-verify}")
private String servicePathVerify;
@BeforeAll
public void init() {
Properties props = System.getProperties();
props.forEach((k, v) -> LOGGER.debug("Prop: {} - {}", k, v));
}
@Test
public void testServiceVerify() {
final ResponseEntity<String> response = assertDoesNotThrow(
() -> executeRequest("testServiceVerify",
createUrl(this.servicePathVerify, null, null),
HttpMethod.GET,
createHttpHeaders(MediaType.TEXT_PLAIN)));
assertEquals(
HttpStatus.OK, response.getStatusCode(),
"Expected Response status OK, statusCode "
+ response.getStatusCode());
assertTrue(doesContainKeywords(response.getBody(), "Verification ok "));
}
1条答案
按热度按时间yx2lnoni1#
解决方案是在测试中添加: