File.delete()不会在模拟时删除文件

时间:2018-10-26 19:29:25

标签: android unit-testing junit mockito powermockito

我正在尝试测试类中的一种方法,该方法实际上是从SD卡删除图像文件。我为此创建了一个模拟环境(运行该程序时基本上是在本地计算机中创建一个文件夹,还将图像保留在同一文件夹中),但是测试用例没有通过(它不会从本地计算机中删除文件系统)。我试图检查if-else条件,这将是else(找不到文件)的情况。谁能帮我解决这个问题,并告诉您确切的问题是什么(为什么它没有获取本地系统路径)。

这是我的考试班:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Environment.class, File.class})
public class DataInitializerTest {


    private DataInitializer dataInitializer;

    @Mock
    private List<String> listOfMockFiles;
    @Mock
    private DataInitializer.OnUpdateDataListener onUpdateDataMockListener;
    @Mock
    private File mDirectory;

    @Mock
    private Context context;
    @Mock
    private SharedPreferences sharedPreferences;
    @Mock
    private SharedPreferences.Editor mEditor;

    /**
     * Creating all the pre Setup before starting the unit testing
     * Mock to all the dependencies {@link SharedPreferences}{@link SharedPreferences.Editor}, {@link Context}
     * @throws IOException
     */
    @Before
    public void createSetup() throws IOException {
        // Get a reference to the class under test
        this.sharedPreferences = Mockito.mock(SharedPreferences.class);
        this.context = Mockito.mock(Context.class);
        this.mEditor = Mockito.mock(SharedPreferences.Editor.class);
        mDirectory= new File("tmp/");
        mDirectory.mkdirs();

        Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPreferences);
        Mockito.when(sharedPreferences.edit()).thenReturn(mEditor);
        dataInitializer = new DataInitializer(context);

        withStaticallyMockedEnvironmentAndFileApis();
    }
    //TODO It passes the failed condition actually, but it does not delete the images from the local system (need to resolve it)
    // TODO as it is not able to mock the file path from the local computer
    @Test
    public void deleteThumbnailsTest() throws IOException {
        // When file helper is asked to create a file
        listOfMockFiles = new ArrayList<>();
        listOfMockFiles.add("ML0180723150936BE999E111");
        listOfMockFiles.add("ML0180723150936BE999E716");
        listOfMockFiles.add("ML0180723150936BE999E717");
        listOfMockFiles.add("ML0180723150936BE999E788");

        dataInitializer.deleteThumbnails(listOfMockFiles,onUpdateDataMockListener);
    }


    private void withStaticallyMockedEnvironmentAndFileApis() throws IOException {


        // Setup mocking for Environment and File classes
        mockStatic(Environment.class, File.class);

        // Make the Environment class return a mocked external storage directory
        when(Environment.getExternalStorageDirectory())
                .thenReturn(mDirectory);

        when(Environment.getExternalStorageState())
                .thenReturn(Environment.MEDIA_MOUNTED);

        Mockito.doNothing().when(onUpdateDataMockListener).onDeleteNotifyAdapter();


    }
    }

我有删除另一个Java类中文件的方法,

public class DataInitializer {

    private SharedPreferences mSharedPreferences;

    public DataInitializer(Context context) {
        mSharedPreferences = context.getSharedPreferences(Constants.FILTER_IMAGE,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(Constants.FILTER_IMAGE, "all");
        editor.apply();
    }

    /*Delete the thumbnails on user selection*/
    /**
     *
     * @param filenames image names from the SD card
     * @param onUpdateDataListener Listener from the presenter to notify on Delete action
     */
    public void deleteThumbnails(List<String> filenames,OnUpdateDataListener onUpdateDataListener){

        for(int i=0;i<filenames.size();i++)
        {
            File fileThumbnail = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"moleculightThumbnail"+File.separator+"thumbnail_" + filenames.get(i));
            fileThumbnail.delete();
            File actualImage = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/moleculight/" + filenames.get(i));
            actualImage.delete();
            onUpdateDataListener.onDeleteNotifyAdapter();

        }

    }
}

0 个答案:

没有答案