【发布时间】:2017-05-30 14:55:51
【问题描述】:
结合使用 testng 和 jmockit 进行一些单元测试。在我正在测试的方法中,它尝试访问我使用 JBoss 部署脚本设置的系统属性,因此我的单元测试无法访问整个 JBoss 环境来访问属性,因此在测试该方法时它返回 null .尝试在我的测试中直接模拟和设置系统变量,但系统属性在我正在测试的类中仍然返回 null。
被测试的类:
//returns the correct value in the application but returns null for the test
public static final String webdavrootDir = System.getProperty("property.name");
public String getFileUrl(){
StringBuilder sb = new StringBuilder();
return sb.append(webdavrootDir)
.append(intervalDir)
.append(fileName)
.toString();
}
测试:
@Test
public void getUrl(@Mocked System system){
system.setProperty("property.name", "https://justatest.com/dav/bulk");
String fileUrl = csvConfig.getFileUrl();
assertEquals(fileUrl, "https://justatest.com/dav/bulk/otherstuff");
}
测试找到值null/otherstuff,但期望值https://justatest.com/dav/bulk/otherstuff
我也尝试在 testng @BeforeMethod 方法中设置系统属性,但没有成功。
【问题讨论】:
标签: java unit-testing testng jmockit system-properties