FileControlImpl implements FileControl { @Override public void deleteFile(String srcFile) { FileUtility.deleteFile(getAbsolutePath(srcFile)); }  @Override void copyFile(String srcFile, String newFile); @Override void deleteFile(String srcFile); @Override void mergeFile(List<String> fileList, String newFile); @Override void renameFile(String srcFile, String newFile); String getBasePath(); void setBasePath(String basePath); void setCheckFileExist(boolean checkFileExist);  }
@Test public void testDeleteFile01() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); String classFileName = this.getClass().getSimpleName() + ".class"; URL url = this.getClass().getResource(classFileName); String directoryPath = url.getPath().substring(0, url.getPath().length() - classFileName.length()); String srcFile = directoryPath + "testDeleteFile01_src.txt"; File file = new File(srcFile); file.delete(); file.createNewFile(); String basePath = ""; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "deleteFile", fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.deleteFile(srcFile); PowerMockito.verifyPrivate(fileControl).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); String argGetAbsolutePath = fileControlStringArgumentCaptor .getValue(); assertEquals(srcFile, argGetAbsolutePath); PowerMockito.verifyStatic(); FileUtility.deleteFile(Mockito.anyString()); String argDeleteFile = fileUtilityStringArgumentCaptor.getValue(); assertEquals(srcFile, argDeleteFile); } finally { file = new File(srcFile); file.delete(); } }
@Test public void testDeleteFile02() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); String classFileName = this.getClass().getSimpleName() + ".class"; URL url = this.getClass().getResource(classFileName); String directoryPath = url.getPath().substring(0, url.getPath().length() - classFileName.length()); String srcFile = "testDeleteFile02_src.txt"; File file = new File(directoryPath + srcFile); file.delete(); file.createNewFile(); String basePath = directoryPath; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "deleteFile", fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.deleteFile(srcFile); PowerMockito.verifyPrivate(fileControl).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); String argGetAbsolutePath = fileControlStringArgumentCaptor .getValue(); assertEquals(srcFile, argGetAbsolutePath); PowerMockito.verifyStatic(); FileUtility.deleteFile(Mockito.anyString()); String argDeleteFile = fileUtilityStringArgumentCaptor.getValue(); assertEquals(basePath + srcFile, argDeleteFile); } finally { file = new File(directoryPath + srcFile); file.delete(); } }
@Test public void testDeleteFile03() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); String classFileName = this.getClass().getSimpleName() + ".class"; URL url = this.getClass().getResource(classFileName); String directoryPath = url.getPath().substring(0, url.getPath().length() - classFileName.length()); String srcFile = directoryPath + "testDeleteFile03_src.txt"; File file = new File(srcFile); file.delete(); file.createNewFile(); String basePath = directoryPath; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "deleteFile", fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.deleteFile(srcFile); PowerMockito.verifyPrivate(fileControl).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); String argListGetAbsolutePath = fileControlStringArgumentCaptor .getValue(); assertEquals(srcFile, argListGetAbsolutePath); PowerMockito.verifyStatic(); FileUtility.deleteFile(Mockito.anyString()); String argDeleteFile = fileUtilityStringArgumentCaptor.getValue(); assertEquals(srcFile, argDeleteFile); } finally { file = new File(srcFile); file.delete(); } }
@Test public void testDeleteFile04() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); String classFileName = this.getClass().getSimpleName() + ".class"; URL url = this.getClass().getResource(classFileName); String directoryPath = url.getPath().substring(0, url.getPath().length() - classFileName.length()); String srcFile = "testDeleteFile04_src.txt"; File file = new File(directoryPath + srcFile); file.delete(); file.createNewFile(); String basePath = ""; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "deleteFile", fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.deleteFile(srcFile); PowerMockito.verifyStatic(); FileUtility.deleteFile(Mockito.anyString()); String argDeleteFile = fileUtilityStringArgumentCaptor.getValue(); assertEquals(basePath + srcFile, argDeleteFile); } finally { file = new File(directoryPath + srcFile); file.delete(); } }
@Test public void testDeleteFile05() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); String srcFile = null; String basePath = ""; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(FileUtility.class, "deleteFile", fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.deleteFile(srcFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { PowerMockito.verifyPrivate(fileControl).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); PowerMockito.verifyStatic(Mockito.never()); FileUtility.deleteFile(Mockito.anyString()); assertEquals(FileException.class, e.getClass()); assertEquals("File name is not set.", e.getMessage()); assertNull(e.getFileName()); } }