Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为采用文件路径并创建新文件的方法编写Junit测试?_Java_Unit Testing_Junit_Mockito - Fatal编程技术网

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

我在Java类中有一个方法,它接受一个文件路径并创建一个新文件。如何为该方法编写Junit测试

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);

我认为应该创建一个测试文件,检查它是否存在,删除它并断言它是否存在。验证文件是否存在,内容是否是您期望的内容。您确定此方法会创建一个新文件吗?在我看来,它只会在文件已经存在的情况下执行任何操作。