1

我遇到了一个无法解决的问题,因为 Internet 上的相关信息太少。所以这就是我想要做的:

File我在测试类的setUp()部分创建了一个。

@Before
    public void setUp() {
        activity = new MainActivity();
        TestUtils.copyAsset(activity, "test.zip");
        File archive = new File(Environment.getExternalStorageDirectory(), "/test.zip");
        archiveManager = new ArchiveManager(archive, ArchiveType.ZIP);
    }

然后我运行一个测试,启动一个AsyncTask通过侦听器返回结果的测试。

@Test
public void testExtractFiles() {
    ZipEntry entry = ((ZipArchive) archiveManager.getArchive()).getZipFile().getEntry(
            "test.pdf");
    ZipEntryFile zef = new ZipEntryFile(Environment.getExternalStorageDirectory() + "test.pdf",
            entry.getName(), new EntryWrapper(new ParseableZipEntry(entry)));
    OnFileExtractedListener listener = new OnFileExtractedListener() {

        @Override
        public void onFileExtracted(File file, boolean open) {
            System.out.println("==================extract");
            Assert.assertNotNull("file not null", file);
            Assert.assertTrue("file exists", file.exists());
            Assert.assertTrue("don't open", !open);
        }

        @Override
        public void onExtractionCompleted() {
        }
    };
    ArrayList<File> files = new ArrayList<File>();
    files.add(zef);

    archiveManager.extractFiles(activity, files, Environment.getExternalStorageDirectory(),
            listener);
}

测试运行良好,现在我需要删除File我创建的,所以我使用以下代码

@After
public void tearDown() {
    try {
        FileUtils.cleanDirectory(Environment.getExternalStorageDirectory());
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(Arrays.toString(Environment.getExternalStorageDirectory().list()));
}

文件没有被删除,我得到以下异常:

java.io.IOException: Unable to delete file: C:\Users\user\AppData\Local\Temp\android-external-cache\test.zip
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:2279)
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1653)
at com.foo.bar.manager.ArchiveManagerTest.tearDown(ArchiveManagerTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:292)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

有什么办法可以克服这个问题?我绝对确定我遗漏了一些重要的东西,但正如我所说,关于这个主题的信息不足。提前致谢。

4

0 回答 0