使用 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
方法中设置系统属性,但没有成功。
确保System.setProperty("property.name", "https://justatest.com/dav/bulk");
在被测试的类被实例化之前调用,否则静态字段将始终为空。
考虑@BeforeClass
为此使用设置方法:
@BeforeClass
public void setup() {
System.setProperty("property.name", "https://justatest.com/dav/bulk");
// Instantiate your CsvConfig instance here if applicable.
}
然后
@Test
public void getUrl(){
System.setProperty("property.name", "https://justatest.com/dav/bulk");
String fileUrl = csvConfig.getFileUrl();
assertEquals(fileUrl, "https://justatest.com/dav/bulk/otherstuff");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句