Automated tests Zerocode:在主机配置文件中设置系统属性
配置: 零码tdd.1.3.2 ${host} 在运行时,使用-D java选项设置系统属性。一切都很好 问题/我需要什么: 在单元测试时,系统属性未设置,主机未解析。 应用程序使用Junit和Zerocode,只需配置Zerocode即可设置系统属性 例如: 主机属性Automated tests Zerocode:在主机配置文件中设置系统属性,automated-tests,integration-testing,junit5,zerocode,Automated Tests,Integration Testing,Junit5,Zerocode,配置: 零码tdd.1.3.2 ${host} 在运行时,使用-D java选项设置系统属性。一切都很好 问题/我需要什么: 在单元测试时,系统属性未设置,主机未解析。 应用程序使用Junit和Zerocode,只需配置Zerocode即可设置系统属性 例如: 主机属性 web.application.endpoint.host:${host} web.application.endpoint.port= web.application.endpoint.context= 更多信息: 要求仅用
web.application.endpoint.host:${host}
web.application.endpoint.port=
web.application.endpoint.context=
更多信息:
要求仅用于配置。无法将新Java代码或条目引入IDE
有人帮忙吗?欢迎提出任何想法。找到了解决方案,但不确定这是否是正确的方法 步骤1:创建配置文件并加载系统属性 Config.java
public class Config {
public Map<String, Object> readProperties(String optionalString) {
Map<String, Object> propertiesMap = new HashMap<>();
final String host = System.getProperty("host");
propertiesMap.put("host", host);
return propertiesMap;
}
}
步骤3:在步骤配置中使用loaded属性
test.json
{
"scenarioName": "Test ...",
"steps": [
{
"name": "config",
"url": "com.test.Config",
"operation": "readProperties",
"request": "",
"assertions": {}
}
]
}
{
"scenarioName": "Test ...",
"steps": [
{
"name": "config",
"url": "com.test.Config",
"operation": "readProperties",
"request": "",
"assertions": {}
},
{
"name": "test",
"url": "${$.config.response.host}/test/xxx",
"operation": "GET",
"request": {},
"assertions": {
"status": 200
}
}
]
}
就是这样,虽然它是有效的,但我正在寻找更好的方法
我正在尝试的一些可能的选择是:
- 加载/配置的通用步骤(在一个位置)
- 在json文件中直接使用属性作为{host}
- 自定义客户端
再次感谢您的帮助/想法。此功能在zerocode版本
1.3.9
及更高版本中提供。
请使用类似于${SYSTEM.PROP:host}
的占位符,例如${SYSTEM.PROPERTY:java.vendor}
解析为Oracle Corporation
或Azul Systems,Inc.
示例链接:
我的问题是,为什么要尝试访问实际的主机/端口?很抱歉回答得太长,但请耐心听我说。我认为有一个更简单的方法来实现你正在尝试的目标。我发现最好从两个方面考虑零代码的使用
protected String urlWithPort;
@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort());
protected String getUriWithPort() {
return "http://localhost:" + wireMockRule.port();
}
注:以上测试使用WireMock版本2.27.1和ZeroCode 1.3.27
希望这能帮助您回答如何为测试动态获取服务器/端口