Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JUNIT的Android文件操作测试_Java_Android_Eclipse_Junit - Fatal编程技术网

Java 使用JUNIT的Android文件操作测试

Java 使用JUNIT的Android文件操作测试,java,android,eclipse,junit,Java,Android,Eclipse,Junit,我正在尝试用我的应用程序测试文件操作。首先,我想检查一下,每当我调用一个读取文件的函数时,这个函数都会抛出一个异常,因为文件不在那里 然而,我似乎不明白如何做到这一点。。。这是我设计的代码,但它不运行。。。正常的JUNIT说找不到文件路径,android JUNIT说测试无法运行 文件夹:/data/data/example.triage/files/已在虚拟设备中可用 @Before public void setUp() throws Exception { dr = new Da

我正在尝试用我的应用程序测试文件操作。首先,我想检查一下,每当我调用一个读取文件的函数时,这个函数都会抛出一个异常,因为文件不在那里

然而,我似乎不明白如何做到这一点。。。这是我设计的代码,但它不运行。。。正常的JUNIT说找不到文件路径,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));
    }

}
@之前
public void setUp()引发异常{
dr=新数据读取器();
dw=新数据编写器();
DefaultValues.file_path_folder=“/data/data/example.triage/files/”;
}
@之后
public void tearDown()引发异常{
dr=null;
dw=null;
//删除我们可能在测试中创建的患者文件。
dr.removeFile(DefaultValues.patients\u file\u path);
}
@试验
public void readHealthCardsNonExistentPatientsFile(){
试一试{
List healthcards=dr.getHealthCardsofPatients();
失败(“当找不到文件时,该方法没有生成异常。”);
}捕获(例外e){
assertTrue(e.getClass().equals(FileNotFoundException.class));
}
}

看起来您并没有以与JUnitAPI相关的方式检查异常

您是否已尝试拨打此电话:

@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()
code不会引发异常,并使用适当的方法将生成异常的代码移动到测试方法中。

setup不会生成异常。。。它只是创建了我正在检查的对象的新实例..好的,太好了。我要删除
setup()
方法上的
throws Exception
标记,因为您不需要它。重写
tearDown()
是否解决了JUnit问题?我意识到问题在于测试的工作方式。。。为了测试Android应用程序的行为,您需要一个新的应用程序来打开待测试的应用程序并分析变量。为此,我创建了一个新项目,解决了这个问题。