Andulos
Andulos

Reputation: 475

How to write a Junit test for a method that takes a file path and creates a new file?

I have method in Java class that takes a file path and creates a new file. How do I write a Junit test for this method?

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

Upvotes: 0

Views: 827

Answers (1)

djharten
djharten

Reputation: 374

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

Upvotes: 1

Related Questions