2

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""
wittich
  • 2,079
  • 2
  • 27
  • 50

2 Answers2

4

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...

Sormuras
  • 8,491
  • 1
  • 38
  • 64
  • 1
    Naturally I've tried that (I might be stupid but not that stupid) but it does not work for me. The problem is I have double quotes in my values as well. Sorry the question was incomplete, I did not see it in the first place. – Jiří Železný Dec 01 '17 at 12:58
  • OK now when I see it my problem is kind of an edge case - I need to escape a comma after double quotes. I understand if this is never implemented... – Jiří Železný Dec 01 '17 at 13:19
  • 1
    You may specify an entire different delimiter character in the annotation: http://junit.org/junit5/docs/snapshot/api/org/junit/jupiter/params/provider/CsvFileSource.html#delimiter-- But then it is no longer "comma" separated values... – Sormuras Dec 01 '17 at 14:09
  • Ha I didn't know about that, that is exactly the workaround I needed. Thank you and sorry for wasting your time. – Jiří Železný Dec 04 '17 at 09:29
  • Fine, then I'll rewrite the comment as an answer. Glad it helped. – Sormuras Dec 04 '17 at 09:32
3

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))
    );
}
Pramod Yadav
  • 467
  • 8
  • 14