spring - 如何在 testpropertysource 附加属性中获取 SpringBootTest.WebEnvironment.RANDOM_PORT?
问题描述
我正在编写一个使用 的测试SpringBootTest.WebEnvironment.RANDOM_PORT
,但我需要在某个外部 bean 使用的属性中使用此端口值(即我无法更改)。
我尝试使用{local.server.port}
:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = {
"externalProp.accessUrl=http://localhost:{local.server.port}}/foo"
})
public class MyTest {
...
}
但我明白了java.lang.IllegalArgumentException: Not enough variable values available to expand 'local.server.port'
我怎样才能做到这一点?
解决方案
你试过 ${local.server.port} 吗?否则,您可以使用 System.xml 设置属性externalProp.accessUrl。
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyTest {
@LocalServerPort
private static int randomServerPort;
@Before
public void setUp(){
System.setProperty("externalProp.accessUrl","http://localhost:"+randomServerPort+"/foo");
}
}
推荐阅读
- arrays - 如何在 VBA 中批量切片数组
- python - 用python覆盖在其他窗口/桌面上
- r - R 循环行为
- java - Intellij:使用“Run '[mainClass].main()' 执行 gradle 项目失败
- c++ - 通过 lambda 中的显式 this 参数访问捕获的变量
- python - 如何动态查找工作目录外的文件路径?
- docker - 无法删除 python 和 ubuntu docker 图像
- jquery - AJAX post call 在我的 JSON 文件中存储两次数据
- html - 在 Visual Studio 中将 HTML 文件与 CSS 文件链接
- mysql - 使用 React.js 和 MySQL 作为数据库的 ASP.NET Core 5.0 应用程序