@@ -1332,32 +1332,13 @@ include::{testDir}/example/ExternalMethodSourceDemo.java[tags=external_MethodSou
1332
1332
1333
1333
`@CsvSource` allows you to express argument lists as comma-separated values (i.e., CSV
1334
1334
`String` literals). Each string provided via the `value` attribute in `@CsvSource`
1335
- represents a CSV line and results in one invocation of the parameterized test.
1335
+ represents a CSV record and results in one invocation of the parameterized test.
1336
1336
1337
1337
[source,java,indent=0]
1338
1338
----
1339
1339
include::{testDir}/example/ParameterizedTestDemo.java[tags=CsvSource_example]
1340
1340
----
1341
1341
1342
- If the programming language you are using supports _text blocks_ -- for example, Java SE
1343
- 15 or higher -- you can alternatively use the `textBlock` attribute of `@CsvSource`. Each
1344
- line within a text block represents a CSV line and results in one invocation of the
1345
- parameterized test. Using a text block, the previous example can be implemented as follows.
1346
-
1347
- [source,java,indent=0]
1348
- ----
1349
- @ParameterizedTest
1350
- @CsvSource(textBlock = """
1351
- apple, 1
1352
- banana, 2
1353
- 'lemon, lime', 0xF1
1354
- strawberry, 700_000
1355
- """)
1356
- void testWithCsvSource(String fruit, int rank) {
1357
- // ...
1358
- }
1359
- ----
1360
-
1361
1342
The default delimiter is a comma (`,`), but you can use another character by setting the
1362
1343
`delimiter` attribute. Alternatively, the `delimiterString` attribute allows you to use a
1363
1344
`String` delimiter instead of a single character. However, both delimiter attributes
@@ -1391,11 +1372,69 @@ by default. This behavior can be changed by setting the
1391
1372
| `@CsvSource(value = { " apple , banana" }, ignoreLeadingAndTrailingWhitespace = false)` | `" apple "`, `" banana"`
1392
1373
|===
1393
1374
1375
+ If the programming language you are using supports _text blocks_ -- for example, Java SE
1376
+ 15 or higher -- you can alternatively use the `textBlock` attribute of `@CsvSource`. Each
1377
+ record within a text block represents a CSV record and results in one invocation of the
1378
+ parameterized test. Using a text block, the previous example can be implemented as follows.
1379
+
1380
+ [source,java,indent=0]
1381
+ ----
1382
+ @ParameterizedTest
1383
+ @CsvSource(textBlock = """
1384
+ apple, 1
1385
+ banana, 2
1386
+ 'lemon, lime', 0xF1
1387
+ strawberry, 700_000
1388
+ """)
1389
+ void testWithCsvSource(String fruit, int rank) {
1390
+ // ...
1391
+ }
1392
+ ----
1393
+
1394
+ In contrast to CSV records supplied via the `value` attribute, a text block can contain
1395
+ comments. Any line beginning with a `+++#+++` symbol will be treated as a comment and
1396
+ ignored. Note, however, that the `+++#+++` symbol must be the first character on the line
1397
+ without any leading whitespace. It is therefore recommended that the closing text block
1398
+ delimiter `"""` be placed either at the end of the last line of input or on the following
1399
+ line, left aligned with the rest of the input (as can be seen in the example below which
1400
+ demonstrates formatting similar to a table).
1401
+
1402
+ [source,java,indent=0]
1403
+ ----
1404
+ @ParameterizedTest
1405
+ @CsvSource(delimiter = '|', quoteCharacter = '"', textBlock = """
1406
+ #-----------------------------
1407
+ # FRUIT | RANK
1408
+ #-----------------------------
1409
+ apple | 1
1410
+ #-----------------------------
1411
+ banana | 2
1412
+ #-----------------------------
1413
+ "lemon lime" | 0xF1
1414
+ #-----------------------------
1415
+ strawberry | 700_000
1416
+ #-----------------------------
1417
+ """)
1418
+ void testWithCsvSource(String fruit, int rank) {
1419
+ // ...
1420
+ }
1421
+ ----
1422
+
1423
+ [NOTE]
1424
+ ====
1425
+ Java's https://docs.oracle.com/en/java/javase/15/text-blocks/index.html[text block]
1426
+ feature automatically removes _incidental whitespace_ when the code is compiled.
1427
+ However other JVM languages such as Groovy and Kotlin do not. Thus, if you are using a
1428
+ programming language other than Java and your text block contains comments or new lines
1429
+ within quoted strings, you will need to ensure that there is no leading whitespace within
1430
+ your text block.
1431
+ ====
1432
+
1394
1433
[[writing-tests-parameterized-tests-sources-CsvFileSource]]
1395
1434
===== @CsvFileSource
1396
1435
1397
1436
`@CsvFileSource` lets you use comma-separated value (CSV) files from the classpath or the
1398
- local file system. Each line from a CSV file results in one invocation of the
1437
+ local file system. Each record from a CSV file results in one invocation of the
1399
1438
parameterized test.
1400
1439
1401
1440
The default delimiter is a comma (`,`), but you can use another character by setting the
@@ -1404,8 +1443,8 @@ The default delimiter is a comma (`,`), but you can use another character by set
1404
1443
cannot be set simultaneously.
1405
1444
1406
1445
.Comments in CSV files
1407
- NOTE: Any line beginning with a `# ` symbol will be interpreted as a comment and will be
1408
- ignored.
1446
+ NOTE: Any line beginning with a `+++#+++ ` symbol will be interpreted as a comment and will
1447
+ be ignored.
1409
1448
1410
1449
[source,java,indent=0]
1411
1450
----
@@ -1418,13 +1457,13 @@ include::{testDir}/example/ParameterizedTestDemo.java[tags=CsvFileSource_example
1418
1457
include::{testResourcesDir}/two-column.csv[]
1419
1458
----
1420
1459
1421
- In contrast to the syntax used in `@CsvSource`, `@CsvFileSource` uses a double quote `"`
1422
- as the quote character. See the `"United States of America"` value in the example above.
1423
- An empty, quoted value `""` results in an empty `String` unless the `emptyValue` attribute
1424
- is set; whereas, an entirely _empty_ value is interpreted as a `null` reference. By
1425
- specifying one or more `nullValues`, a custom value can be interpreted as a `null`
1426
- reference. An `ArgumentConversionException` is thrown if the target type of a `null`
1427
- reference is a primitive type.
1460
+ In contrast to the default syntax used in `@CsvSource`, `@CsvFileSource` uses a double
1461
+ quote `"` as the quote character. See the `"United States of America"` value in the
1462
+ example above. An empty, quoted value `""` results in an empty `String` unless the
1463
+ `emptyValue` attribute is set; whereas, an entirely _empty_ value is interpreted as a
1464
+ `null` reference. By specifying one or more `nullValues`, a custom value can be
1465
+ interpreted as a `null` reference. An `ArgumentConversionException` is thrown if the
1466
+ target type of a `null` reference is a primitive type.
1428
1467
1429
1468
NOTE: An _unquoted_ empty value will always be converted to a `null` reference regardless
1430
1469
of any custom values configured via the `nullValues` attribute.
0 commit comments