Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 1005 characters in body
Source Link

I'm trying to write unit tests. The unit tests have input of a stringed csv file. So I'm having csvFileDataAsString as the simulated input.

String csvFileDataAsString = 'Column1,Column2,Column3\n00301,true,false\n00302,false,false'

The unit test is failing. With debug prints, I found: List<String> data = csvFileDataAsString.split('\n'); ---> data.size()= 1??? List<String> data = csvFileDataAsString.split('\\n'); ---> data.size()still = 1???

List<String> columndata = caseContactsData[0].split(','); --> columndata.size() = 7 ??

Seems like the split by new line was not done properly in my dummy test string. How should I define the test string properly please? And should I use split by \n or \\n please?

Thank you.

-------------------------Update with complete code ------

List<Object__c> readObjectCSVFile(String csvFileDataAsString) {
        List<Object__c> Objects = new List<Object__c>();
        List<String> ObjectsData = csvFileDataAsString.split('\n');
        // Also tried List<String> ObjectsData = csvFileDataAsString.split('\\n'); but size still gives as 1

        System.debug(ObjectsData.size()); ---> 1 (instead of excepted 3 lines)

        if (csvFileDataAsString.length() == 0 || ObjectsData.isEmpty()) {
            showError('readObjectCSVFile: CSV file is empty');
            return Objects;
        }

        List<String> columnNames = ObjectsData[0].split(',');
        System.debug(columnNames.size()); ---> 7 (instead of excepted 3 cells in a row)

        // more process logic based on cell data...
        }
        return Objects;
    }

input string was String csvFileDataAsString = 'Column1,Column2,Column3\n00301,true,false\n00302,false,false'

I'm trying to write unit tests. The unit tests have input of a stringed csv file. So I'm having csvFileDataAsString as the simulated input.

String csvFileDataAsString = 'Column1,Column2,Column3\n00301,true,false\n00302,false,false'

The unit test is failing. With debug prints, I found: List<String> data = csvFileDataAsString.split('\n'); ---> data.size()= 1??? List<String> data = csvFileDataAsString.split('\\n'); ---> data.size()still = 1???

List<String> columndata = caseContactsData[0].split(','); --> columndata.size() = 7 ??

Seems like the split by new line was not done properly in my dummy test string. How should I define the test string properly please? And should I use split by \n or \\n please?

Thank you.

I'm trying to write unit tests. The unit tests have input of a stringed csv file. So I'm having csvFileDataAsString as the simulated input.

String csvFileDataAsString = 'Column1,Column2,Column3\n00301,true,false\n00302,false,false'

The unit test is failing. With debug prints, I found: List<String> data = csvFileDataAsString.split('\n'); ---> data.size()= 1??? List<String> data = csvFileDataAsString.split('\\n'); ---> data.size()still = 1???

List<String> columndata = caseContactsData[0].split(','); --> columndata.size() = 7 ??

Seems like the split by new line was not done properly in my dummy test string. How should I define the test string properly please? And should I use split by \n or \\n please?

Thank you.

-------------------------Update with complete code ------

List<Object__c> readObjectCSVFile(String csvFileDataAsString) {
        List<Object__c> Objects = new List<Object__c>();
        List<String> ObjectsData = csvFileDataAsString.split('\n');
        // Also tried List<String> ObjectsData = csvFileDataAsString.split('\\n'); but size still gives as 1

        System.debug(ObjectsData.size()); ---> 1 (instead of excepted 3 lines)

        if (csvFileDataAsString.length() == 0 || ObjectsData.isEmpty()) {
            showError('readObjectCSVFile: CSV file is empty');
            return Objects;
        }

        List<String> columnNames = ObjectsData[0].split(',');
        System.debug(columnNames.size()); ---> 7 (instead of excepted 3 cells in a row)

        // more process logic based on cell data...
        }
        return Objects;
    }

input string was String csvFileDataAsString = 'Column1,Column2,Column3\n00301,true,false\n00302,false,false'

Source Link

CSV file string parsing by newline error

I'm trying to write unit tests. The unit tests have input of a stringed csv file. So I'm having csvFileDataAsString as the simulated input.

String csvFileDataAsString = 'Column1,Column2,Column3\n00301,true,false\n00302,false,false'

The unit test is failing. With debug prints, I found: List<String> data = csvFileDataAsString.split('\n'); ---> data.size()= 1??? List<String> data = csvFileDataAsString.split('\\n'); ---> data.size()still = 1???

List<String> columndata = caseContactsData[0].split(','); --> columndata.size() = 7 ??

Seems like the split by new line was not done properly in my dummy test string. How should I define the test string properly please? And should I use split by \n or \\n please?

Thank you.