Testing file creation in Robolectric

I am trying to read a test in robolectric on Android. I basically have a class that writes serialized objects to a file. I pass the context to the constructor and use it to call the methodgetFilesDir()

new File(context.getFilesDir(), filename); 

I pass RuntimeEnvironment.applicationas context when testing, but I get the message assert failed when I call the method below to check if the file exists.

@Override
public boolean fileExists() {

    return context.getFileStreamPath(filename).exists();
} 

I was wondering if there is another way around this and use the test context to create the file. Thanks in advance!

here is a simple test iam doing

@Test
public void testFileCreated()
{
    objmodelroom.createFile();
    assertTrue(objmodelroom.fileExists());
}

I am creating a file like this

public void createFile()
{
    this.objectfile = new File(context.getFilesDir(), filename);
    objectfile.setReadable(true);
    objectfile.setWritable(true);
}
+4
source share

Source: https://habr.com/ru/post/1618200/


All Articles