我有大约15个JUnit测试用例,每个测试用例需要一个差异资源文件,从中读取必要的输入数据.目前,我正在硬编码每个测试用例方法中的特定资源文件路径.
@Test
public void testCase1() {
URL url = this.getClass().getResource("/resource1.txt");
// more code here
}
@Test
public void testCase2() {
URL url = this.getClass().getResource("/resource2.txt");
// more code here
}
Run Code Online (Sandbox Code Playgroud)
可能是我可以将setUp()方法中加载的所有这些文件放入单独的URL变量中,然后在每个测试方法中使用特定的URL变量.有没有更好的方法来做到这一点?
您可以使用该TestName规则.
@Rule public TestName testName = new TestName();
public URL url;
@Before
public void setup() {
String resourceName = testName.getMethodName().substring(4).toLowerCase();
url = getClass().getResource("/" + resourceName + ".txt");
}
@Test
public void testResource1() {
// snip
}
@Test
public void testResource2() {
// snip
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46 次 |
| 最近记录: |