JUnit5 CsvFileSource - escape comma (after double quotes)

I haven't found a way to escape the , character in CSV resource file that is used by CsvFileSource junit5 annotation. Consequently, any string containing comma is cut in half and the second part is never used.

Is there some workaround for this?


EDIT: The original question was not complete. The problem is I have commas and double quotes in my resource. Parametrized tests deal with the quotes but not with both.

Example CSV line:

5,10,5,53,"</identity/partners?limit=5&cursor=5>; rel="prev", </identity/partners?limit=5&cursor=15>; rel="next""

Quotes are escaped correctly but only until a wild comma appears (that is the algorithm I might guess).

So the resulting assert looks like this:

"Link" was not "\"</identity/partners?limit=5&cursor=5>; rel=\"prev\"", was "</identity/partners?limit=5&cursor=5>; rel="prev", </identity/partners?limit=5&cursor=15>; rel="next""

Upvotes: 6

Views: 6738

Answers (3)

Yury Volkov
Yury Volkov

Reputation: 53

You should use the quote character to quote values with commas. By default quote character is single quote ', like this:

@CsvSource({
  "apple,1",
  "'lemon, lime',0xF1"})

As in example, "lemon, lime" will be treated as single value. To change the quote character, use the quoteCharacter parameter:

@CsvSource({
  "apple,1",
  "\"lemon, lime\",0xF1"}, quoteCharacter = '\"')

Documentation: https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-sources-CsvSource

Upvotes: 1

Pramod Yadav
Pramod Yadav

Reputation: 557

If someone lands in this page looking for an answer, here is an example I used in my tests to meet the goal to be able to use these string values in my test (Note: it doesn’t escapes comma but achieves goal to run tests that includes comma in input. You can use any delimiter you wish).

@ParameterizedTest(name = "Product details for product number - {0}")
@CsvSource(value = {"0; Sauce Labs Backpack;carry.allTheThings() with the sleek, streamlined Sly Pack that melds uncompromising style with unequaled laptop and tablet protection."
        ,"1; Sauce Labs Bike Light;A red light isn't the desired state in testing but it sure helps when riding your bike at night. Water-resistant with 3 lighting modes, 1 AAA battery included."
}, delimiter = ';')
void assertThatProductDescriptionIsCorrectForAStandardUser(Integer productNumber, String productSummary, String productDescription) {
    String url = String.format("%s/%s", swagItemDetails, productNumber);
    deepLink.deepLinkToScreen(url , packageName);

    assertAll("Product Details"
            , () -> assertEquals(productSummary, productScreen.getProductSummary())
            , () -> assertEquals(productDescription, productScreen.getProductDescriptionByText(productDescription))
    );
}

Upvotes: 7

Sormuras
Sormuras

Reputation: 9089

Wrap your value in double quotes: "first, value", second, third and last one

More details here: http://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-sources-CsvFileSource

In your case, you may specify an entire different delimiter character in the annotation: https://junit.org/junit5/docs/current/api/org.junit.jupiter.params/org/junit/jupiter/params/provider/CsvFileSource.html#delimiter()

But then it is no longer "comma" separated values...

Upvotes: 4

Related Questions