如何为采用文件路径并创建新文件的方法编写Junit测试?

问题描述 投票:0回答:1

我在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);
    }
}
java unit-testing junit mockito
1个回答
0
投票
@Test
void testValidFilePath() {
  String filePath = "path.txt";
  String str = sendToECM(filePath);
  Assertions.assertNotEquals(str, ""); // or null, depending on what your StringUtils.EMPTY is doing

@Test
void testInvalidFilePath() {
  String filePath = "invalidPath.txt";
  String str = sendtoECM(filePath);
  Assertions.assertEquals(str, StringUtils.EMPTY);
© www.soinside.com 2019 - 2024. All rights reserved.