Java 如何为采用文件路径并创建新文件的方法编写Junit测试?
我在Java类中有一个方法,它接受一个文件路径并创建一个新文件。如何为该方法编写Junit测试Java 如何为采用文件路径并创建新文件的方法编写Junit测试?,java,unit-testing,junit,mockito,Java,Unit Testing,Junit,Mockito,我在Java类中有一个方法,它接受一个文件路径并创建一个新文件。如何为该方法编写Junit测试 public String sendToECM(String filePath) { String ecmDocId = StringUtils.EMPTY; try { File file = new File(filePath); if(file.exists()) { DocumentPropertiesVO docume
public String sendToECM(String filePath) {
String ecmDocId = StringUtils.EMPTY;
try {
File file = new File(filePath);
if(file.exists()) {
DocumentPropertiesVO documentPropertiesVO = UploadFileToContentManagement.uploadDocument(file);
ecmDocId = documentPropertiesVO.getDocumentID();
}
return ecmDocId;
} catch (SomeException cee) {
log.error("There was an error adding the document", cee);
}
}
@测试
void testValidFilePath(){
字符串filePath=“path.txt”;
字符串str=sendToECM(文件路径);
Assertions.assertNotEquals(str,“”;//或null,具体取决于StringUtils.EMPTY正在执行的操作
@试验
void testInvalidFilePath(){
字符串filePath=“invalidPath.txt”;
字符串str=sendtoECM(文件路径);
Assertions.assertEquals(str,StringUtils.EMPTY);
我认为应该创建一个测试文件,检查它是否存在,删除它并断言它是否存在。验证文件是否存在,内容是否是您期望的内容。您确定此方法会创建一个新文件吗?在我看来,它只会在文件已经存在的情况下执行任何操作。