我遇到了一个无法解决的问题,因为互联网上的信息太少了。所以这就是我要做的事情:
我在测试类的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)
有什么方法可以解决这个问题吗?我绝对相信我错过了一些重要的东西,但正如我所说,关于这个话题的信息不足。提前谢谢。