我正在尝试使用我的APP测试文件操作。首先,我想检查一下,每当我调用一个读取文件的函数时,此函数将引发异常,因为文件不存在。
但是,我似乎不明白如何实现此目标。这是我设计的代码,但是无法运行...普通的JUNIT表示未找到FILEPATH,Android JUNIT则表示“测试”无法运行。
文件夹/data/data/example.triage/files/在虚拟设备中已经可用...
@Before
public void setUp() throws Exception {
dr = new DataReader();
dw = new DataWriter();
DefaultValues.file_path_folder = "/data/data/example.triage/files/";
}
@After
public void tearDown() throws Exception {
dr = null;
dw = null;
// Remove the patients file we may create in a test.
dr.removeFile(DefaultValues.patients_file_path);
}
@Test
public void readHealthCardsNonExistentPatientsFile() {
try {
List<String> healthcards = dr.getHealthCardsofPatients();
fail("The method didn't generate an Exception when the file wasn't found.");
} catch (Exception e) {
assertTrue(e.getClass().equals(FileNotFoundException.class));
}
}
看起来您不是在以与JUnit API相关的方式检查异常。
您是否尝试拨打电话:
@Test (expected = Exception.class)
public void tearDown() {
// code that throws an exception
}
我认为您不希望该setup()
函数能够生成异常,因为在所有其他测试用例之前都会调用该异常。
这是测试异常的另一种方法:
Exception occurred = null;
try
{
// Some action that is intended to produce an exception
}
catch (Exception exception)
{
occurred = exception;
}
assertNotNull(occurred);
assertTrue(occurred instanceof /* desired exception type */);
assertEquals(/* expected message */, occurred.getMessage());
因此,我将使您的setup()
代码不引发异常,并使用适当的方式对其进行测试,将异常生成代码移至测试方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句